[R] subset using noncontiguous variables by name (not index)
François Pinard
pinard at iro.umontreal.ca
Sun Aug 26 23:30:28 CEST 2007
[Muenchen, Robert A (Bob)]
>I'm using the subset function to select a list of variables, some of
>which are contiguous in the data frame, and others of which are not. It
>works fine when I use the form:
>subset(mydata,select=c(x1,x3:x5,x7))
>In reality, my list is far more complex. So I would like to store it in
>a variable to substitute in for c(x1,x3:x5,x7) but cannot get it to
>work. That use of the c function seems to violate R rules, so I'm not
>sure how it works at all. A small simulation of the problem is below.
>mydata <- data.frame(
> x1=c(1,2,3,4,5),
> x2=c(1,2,3,4,5),
> x3=c(1,2,3,4,5),
> x4=c(1,2,3,4,5),
> x5=c(1,2,3,4,5),
> x6=c(1,2,3,4,5),
> x7=c(1,2,3,4,5)
>)
>mydata
># This does what I want.
>summary(subset(mydata, select=c(x1, x3:x5, x7)))
Maybe:
variables <- expression(c(x1, x3:x5, x7))
and later:
summary(subset(mydata, select=eval(variables)))
However, I do not know how one computes the expression piecemeal, that
is, better than by building a string and parsing the result.
--
François Pinard http://pinard.progiciels-bpi.ca
More information about the R-help
mailing list