Iterating and looping over multiple columns in glm in r using a name from another variable

I am trying to iterate over multiple columns for a glm function in R.

names <- names(mtcars[-c(1,2)])

for(i in 1:length(names)){
  print(paste0("Starting iterations for ",names[i]))
  model <-  glm(mpg ~ cyl + paste0(names[i]), data=mtcars, family = gaussian())
  print(paste0("Iterations for ",names[i], " finished"))

however, I am getting the following error:

[1] "Starting iterations for disp"
Error in model.frame.default(formula = mpg ~ cyl + paste0(names[i]), data = mtcars,  : 
  variable lengths differ (found for 'paste0(names[i])')

Not sure, how I can correct this.

>Solution :

mpg ~ cyl + paste0(names[i]) or even mpg ~ cyl + names[i] is not a valid syntax for a formula. Use

reformulate(c("cyl", names[i]), "mpg")

instead, which dynamically creates a formula from variable names.

Leave a Reply