Creating formula using very long strings in R

前端 未结 3 852
挽巷
挽巷 2021-01-19 14:43

I\'m in a situation where I have a vector full of column names for a really large data frame.

Let\'s assume: x = c(\"Name\", \"address\", \"Gender\", ......, \

3条回答
  •  情书的邮戳
    2021-01-19 15:20

    The problem is that you have R keywords as column names. else is a keyword so you can't use it as a regular name.

    A simplified example:

    s <- c("x", "else", "z")
    f <- paste("y~", paste(s, collapse="+"))
    formula(f)
    # Error in parse(text = x) : :1:10: unexpected '+'
    # 1: y~ x+else+
    #              ^
    

    The solution is to wrap your words in backticks "`" so that R will treat them as non-syntactic variable names.

    f <- paste("y~", paste(sprintf("`%s`", s), collapse="+"))
    formula(f)
    # y ~ x + `else` + z
    

提交回复
热议问题