[R] list subsetting question

Matthew Wiener mcw at ln.nimh.nih.gov
Tue Mar 6 17:14:37 CET 2001


Hi, all.

I'm curious about the following behavior in list subsetting:


> t1 <- as.list(1:10)
> t2 <- 1:10
> t2 == 5
 [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE

> t1[t2==5]
[[1]]
[1] 5

> t1[[t2==5]]
Error:  attempting to select more than one element
> t1[[which(t2==5)]]
[1] 5


I don't understand why the second-to-last command gives an error, rather
than the same result as the third-to-last command.  After all, only one
element of the indexing vector is TRUE, so as far as I can tell I'm really
only trying to select one element.

The final command does what I want (as does t1[t2==5][[1]]), so this isn't
an urgent question, but I've looked through the documentation and can't
figure out why t1[[t2==5]] doesn't work.

Thanks for any help.

Matt Wiener



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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