[Solved] vector of characters into expression [closed]


I’m not sure there is enough information in your question for me to give you a specific answer, but here’s a start…

You can extract the variable names from a data.frame called df with:

names_vec <- names(df)

You can get the desired pattern of variable names with plus signs between them with:

string_1 <- paste(names_vec, collapse = " + ")

So far, you probably just have the “right side” of a formula that you would like to feed into a modeling function. Use paste again to “complete” the formula by adding a left side (assuming your dependent variable is called y):

string_2 <- paste("y ~", string_1)

Now while this string vector looks like an R formula, it is actually just a string of characters stored in a length-1 vector. R usually “knows” that it should convert this string into a formula, but if you need to do so manually, just wrap wrap the string vector in formula as in:

lm(formula(string_2), data=df)

It is worth mentioning that if you have 3001 variables in your data.frame (one “y” variable and 3000 “x” variables, for example), then you can simply use a period (“.”) to mean “all other variables” as in:

lm(y ~ ., data=df)

This will achieve the same result as my 4-step method above.

Hopefully this is enough to get you started…

2

solved vector of characters into expression [closed]