mommomonthewind
mommomonthewind

Reputation: 4640

How can I get the list of column fast in R for regression?

I have a dataframe with many columns, and I want to do a regression.

Currently, I have to type:

lm (col3 ~ col4 + col5 + col6 ... + col20)

It is not very convenient. Is there a faster way to do that?

Upvotes: 1

Views: 33

Answers (1)

akrun
akrun

Reputation: 887148

We subset the dataset by selecting the columns that are needed in the regression formula and use . on the RHS of ~ to specify all the columns that are not the dependent variable ('col3').

lm(col3~., subset(df1, select=col3:col20))

data

set.seed(24)
df1 <- as.data.frame(matrix(sample(0:9, 25*40, replace=TRUE), ncol=25))
colnames(df1) <- paste0('col', 1:25)

Upvotes: 3

Related Questions