maccz
maccz

Reputation: 1

use more than 100 features lm function in R

Assume I have a dataframe consisting of 101 columns, where the first 100 are named data1 to data100 and the 101th column is named y.

If I want to use the lm function in R where data1 to data100 are the features.

I know this can be written as:

lin_reg <- lm(y ~ data1+...+data100, dataframe)

Is there a better way of doing this?

Upvotes: 0

Views: 84

Answers (2)

deschen
deschen

Reputation: 11006

lin_reg <- lm(y ~ ., data = dataframe)

This assumes your data is really only consisting of your outcome + all feature variables. No extra column. The "." indicates "take everything else from that data frame".

Since - as per comment - the TO wants to exclude certain columns:

data_frame_subset <- dataframe[, !names(dataframe) %in% c("data5", "data10")]
lin_reg <- lm(y ~ ., data = dataframe_subset)

In this example, I would exclude the columns data5 and data10.

Upvotes: 4

Ronak Shah
Ronak Shah

Reputation: 389175

You can create the formula dynamically with reformulate :

lin_reg <- lm(reformulate(paste0('data', 1:100), 'y'), dataframe)

Upvotes: 0

Related Questions