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
The data object will contain column names that are character but it is easy to coerce them to numeric with the as. numeric() command.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With