evt - 10 months ago 49

R Question

I want to run a regression with a bunch of independent variables from my dataset. There are a lot of predictors, so I do not want to write them all out. Is there a notation to span multiple columns so I don't have to type each?

My attempt was doing this (where my predictors are column 20 to 43):

`modelAllHexSubscales = lm(HHdata$garisktot~HHdata[,20:43])`

Obviously, this does not work because

`HHdata[,20:43]`

`HHdata[,20]+HHdata[,21]`

Answer Source

Generate a formula by pasting column names first.

```
f <- as.formula(paste('garisktot ~', paste(colnames(HHdata)[20:43], collapse='+')))
modelAllHexSubscales <- lm(f, HHdata)
```