I have a dataframe with many variables. I want to apply a linear regression to explain the last one with the others. So as I had to much to write I thought about creating a string with the independent variables e.g. Var1 + Var2 +...+ VarK. I achieved it pasting "+" to all column names except for the last one with this code:
ExVar <- toString(paste(names(datos)[1:11], "+ ", collapse = ''))
I also had to remove the last "+":
ExVar <- substr(VarEx, 1, nchar(ExVar)-2)
So I copied and pasted the ExVar string within the lm() function and the result looked like this:
m1 <- lm(calidad ~ Var1 + Var 2 +...+ Var K)
The question is: Is there any way to use "ExVar" within the lm() function as a string, not as a variable, to have a cleaner code?
For better understanding:
If I use this code:
m1 <- lm(calidad ~ ExVar)
It is interpreting ExVar as a independent variable.
ExVar <- paste(names(datos)[1:11], collapse = ' + '))avoids need to remove "+".lmcan take a string as the formula. Your last line of code isn't working because the whole formula has to be a string, rather than a combination of formula and string. So if your outcome is the variable in, say, the 12th column, you can do:lm(paste(names(datos)[12], " ~ ", ExVar), data=datos). Or,lm(reformulate(ExVar, names(datos)[12]), data=datos). Or you can use a string directly, like"calidad"instead ofnames(datos)[12].ExVar <- paste(names(datos)[1:11], collapse = ' + ').