[R] using a variable for a column name in a formula
p_connolly at slingshot.co.nz
Mon Oct 14 00:05:01 CEST 2013
On 2013-10-14 10:04, David Epstein wrote:
> lm(height ~ ., data=X)
> works fine.
> nnn <- "height" ; lm(nnn ~ . ,data=X)
> How do I write such a formula, which depends on the value of a string
> variable like nnn above?
as.formula() with paste() could work, but from where you are now, try
lm(get(nnn) ~ . ,data=X)
> 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.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help