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

2013-10-14 Thread peter dalgaard
On Oct 13, 2013, at 23:40 , Sarah Goslee wrote: > This being R, there are likely other ways, but I use: > > lm(as.formula(paste(nnn, "~ .")),data=X) > That'll do for most purposes, but fortune("parse") applies. In particular: What happens if nnn is like "weight (kg)"? I'd prefer to do a litt

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

2013-10-13 Thread arun
Hi, I am getting this: #Using an example dataset: set.seed(24)  X <- data.frame(weight=sample(100:250,20,replace=TRUE),height=sample(140:190,20,replace=TRUE)) nnn <- "height" res <- lm(as.formula(paste(nnn, "~.")),data=X)  res2 <- lm(get(nnn) ~ . ,data=X)  coef(res)  (Intercept)   weight

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

2013-10-13 Thread p_connolly
On 2013-10-14 10:04, David Epstein 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? as.formula() with paste() could work, but from where you are now, tr

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

2013-10-13 Thread arun
Sorry, a mistake in the code: #should be "collapse" instead of "sep" res <- lm(formula(paste(nnn,"~",paste(Others, collapse="+"))),data=X) A.K. On Sunday, October 13, 2013 5:55 PM, arun wrote: Hi, May be: set.seed(24)  X <- data.frame(weight=sample(100:250,20,replace=TRUE),height=sample(14

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

2013-10-13 Thread arun
Hi, May be: set.seed(24)  X <- data.frame(weight=sample(100:250,20,replace=TRUE),height=sample(140:190,20,replace=TRUE)) Others <- colnames(X)[!colnames(X)%in%"height"]  nnn <- "height" res <- lm(formula(paste(nnn,"~",paste(Others, sep="+"))),data=X)  res1<- lm(height~.,data=X) #or res2<- lm(g

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

2013-10-13 Thread Sarah Goslee
This being R, there are likely other ways, but I use: lm(as.formula(paste(nnn, "~ .")),data=X) Sarah On Sun, Oct 13, 2013 at 5:04 PM, David Epstein wrote: > lm(height ~ ., data=X) > works fine. > > However > nnn <- "height" ; lm(nnn ~ . ,data=X) > fails > > How do I write such a formula, whic

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

2013-10-13 Thread David Epstein
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 ou