[R] list: index of the element, that is TRUE
Duncan Murdoch
murdoch.duncan at gmail.com
Mon Jan 16 17:15:58 CET 2012
On 12-01-16 10:34 AM, Marion Wenty wrote:
> Dear People,
>
> I have got the following example for a vector and the index of the TRUE
> element:
>
> Myvector<- c(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE)
> which(Myvector)
>
> Now I would like to find out the same for a list:
>
> Mylist<- list(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE)
>
> ...
>
> Does anyone know how to do this?
What are the possible values in your list? If it always contains TRUE
and FALSE and nothing else, then unlist() will work (as suggested by
others). If there are other possibilities, unlist() might mess up, e.g.
unlist( list(TRUE, 1:3, FALSE) )
won't give a vector of length 3. In that case,
which(sapply(MyList, isTRUE) )
might be what you want.
Duncan Murdoch
More information about the R-help
mailing list