[R] using a variable for a column name in a formula

Sarah Goslee sarah.goslee at gmail.com
Sun Oct 13 23:40:09 CEST 2013

This being R, there are likely other ways, but I use:

lm(as.formula(paste(nnn, "~ .")),data=X)


On Sun, Oct 13, 2013 at 5:04 PM, David Epstein
<David.Epstein at warwick.ac.uk> wrote:
> lm(height ~ ., data=X)
> works fine.
> However
> nnn <- "height" ;  lm(nnn ~ . ,data=X)
> fails
> How do I write such a formula, which depends on the value of a string variable like nnn above?
> A typical application might be a program that takes a data frame containing only numerical data, and figures out which of the columns can be best predicted from all the other columns.
> Thanks
> David

Sarah Goslee

More information about the R-help mailing list