On 07/02/2011 10:13 AM, Sean Zhang wrote:
Dear R helper,
I wonder whether there is a quick way to extract some elements for a list.
for a vector we can do the following
vec<- seq(3)
names(vec)<- LETTERS[1:3]
vec[c(1,3)]
vec[c('A','C')]
But for a list,
test.l<- list(c(1,3),array(NA,c(1,2)),array(0,c(2,3)))
names(test.l)<-LETTERS[1:3]
The following does not work. is there some command (I was thinking of
do.call) that can do the job?
test.l[[c('A','B')]]
test.l[[c(1,3)]]
Use single brackets, i.e.
test.l[c('A', 'B')]
test.l[c(1,3)]
The single bracket is the "subsetting" operator, which is what you're
doing here, since you want a list as the result. Double brackets
extract single elements and return whatever type the element is.
Duncan Murdoch
do.call('[',c(test.l,c(1,3)))
do.call('[[',c(test.l,c(1,3)))
do.call('[',c(test.l,c('A','C')))
do.call('[[',c(test.l,c('A','C')))
Thanks in advance.
-Sean
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.