[R] of c, cat, paste, and lists??
Thomas Lumley
tlumley at u.washington.edu
Thu Nov 29 20:33:12 CET 2001
On Thu, 29 Nov 2001, Michaell Taylor wrote:
>
> Now I want the "vote?" variables in a dataframe. I can grep them by
> > grep("vote",ls(),value=T)
> [1] "vote1" "vote10" "vote2" "vote3" "vote4" "vote5" "vote6" "vote7"
> [9] "vote8" "vote9"
>
> seems like some sort of:
> mydata _ data.frame(as.list(grep("vote",ls(),value=T)))
>
> should work, but doesn't. The the list isn't being fed into the data.frame
> function properly, but I can't figure out how it needs to be feed.
>
There are two problems here.
The first is that you want the list of 10 names to be 10 arguments to
data.frame, not one argument as you have here. The second problem is that
you need to convert them from strings to variable names.
If nm<-grep("vote",ls(),value=T)
then the second problem is fixed by using as.name() on the string to turn
them into names
lapply(nm,as.name)
The first problem is fixed with the do.call() function, which takes a
single list and turns its elements into arguments for a function
do.call("data.frame", lapply(nm, as.name))
So for example
R> b<-1:10
R> c<-1:10
R> d<-1:10
R> do.call("data.frame",lapply(c("b","c","d"),as.name))
b c d
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
-thomas
Thomas Lumley Asst. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list