Re: [R] how to generate argument from a vector automatically

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Tue 12 Jul 2005 - 20:52:37 EST

You can do it by subsetting the dataframe

  df <- data.frame( y=rbinom(100, 1, prob=0.5), 
                    x1=rnorm(100), x2=rnorm(100), x3=rnorm(100),
                    z1=rnorm(100), z2=rnorm(100) )
  names <- c("x1", "x2", "x3")
  tree( y ~ . , data = df[ , c("y", names) ] )

This solution is useless when you want to consider interactions between some terms etc. Another more flexible solution is from last example from help(formula) :

   xnam <- paste("x", 1:3, sep="")
   call <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))    tree( call, df )

Regards, Adai

On Tue, 2005-07-12 at 09:53 +0000, zhihua li wrote:
> hi netters
>
> i have a vector NAMES containing a series of variable names:
> NAMES=c(x,r,z,m,st,qr,.....nn).
> i wanna fit a regression tree by using the code:
> my.tree<-tree(y~x+r+z+m+....nn,my.dataframe)
>
> but i don't want to type out "x+r+z+m+....+nn" one by one, as there are so
> many variables. besides, sometimes i wanna put the code in a function. so i
> need to have the argument "x+r+z+m+....+nn" generated from NAMES
> automatically.
>
> i've tried the code: paste(X,collpase="+") but it didn't work.
>
> could anybody give me a hint?
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 12 21:00:02 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:32 EST