Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using column numbers not names in lm()

Tags:

r

formula

lm

Instead of something like lm(bp~height+age, data=mydata) I would like to specify the columns by number, not name.

I tried lm(mydata[[1]]~mydata[[2]]+mydata[[3]]) but the problem with this is that, in the fitted model, the coefficients are named mydata[[2]], mydata[[3]] etc, whereas I would like them to have the real column names.

Perhaps this is a case of not having your cake and eating it, but if the experts could advise whether this is possible I would be grateful

like image 263
LeelaSella Avatar asked Oct 12 '11 15:10

LeelaSella


People also ask

Can a column name be a number in R?

The data object will contain column names that are character but it is easy to coerce them to numeric with the as. numeric() command.

How do you set column numbers in R?

To get number of columns in R Data Frame, call ncol() function and pass the data frame as argument to this function. ncol() is a function in R base package. In this tutorial, we will learn how to use ncol() function to get number of columns in the Data Frame.

How to access a column in an r data frame?

The column items in a data frame in R can be accessed using: Single brackets [] , which would display them as a column. Double brackets [[]] , which would display them as a list. Dollar symbol $ , which would display them as a list.

How to know the column names in r?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in.


1 Answers

lm(
    as.formula(paste(colnames(mydata)[1], "~",
        paste(colnames(mydata)[c(2, 3)], collapse = "+"),
        sep = ""
    )),
    data=mydata
)

Instead of c(2, 3) you can use how many indices you want (no need for for loop).

like image 58
Tomas Avatar answered Oct 13 '22 00:10

Tomas