Skip to content

list: index of the element, that is TRUE

7 messages · Marion Wenty, David Winsemius, R. Michael Weylandt +3 more

#
On Jan 16, 2012, at 10:34 AM, Marion Wenty wrote:

            
which(unlist(Mylist))
David Winsemius, MD
West Hartford, CT
#
I'd just unlist it to a vector and use the same methods: a plus of
this (I believe) is that if you get a vector when a list as expected,
your program will continue to work.

Michael
On Mon, Jan 16, 2012 at 10:34 AM, Marion Wenty <marion.wenty at gmail.com> wrote:
#
On 12-01-16 10:34 AM, Marion Wenty wrote:
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
#
On Mon, Jan 16, 2012 at 10:15 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
Or if you're writing a function:

which(vapply(MyList, isTRUE, logical(1)))

which will work even if length(MyList) == 0

Hadley