[R] list: index of the element, that is TRUE
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? Thank you very much in advance, Marion [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
On Jan 16, 2012, at 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) which(unlist(Mylist)) ... Does anyone know how to do this? Thank you very much in advance, Marion [[alternative HTML version deleted]] __ R-help@r-project.org 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
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.we...@gmail.com 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? Thank you very much in advance, Marion [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
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 __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
Given Mylist - list(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE) Try which(as.logical(MyList)) to return the indices where Mylist is TRUE. [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
Thank you all very much for your help! Yes, the unlist or as.logical command is what I was looking for. Anyway, Duncan, the sapply is also usefull and I might need it some other time. Thanks, Michael, also for the tip that if you get a vector when a list is expected, that the the program will probably continue to work. Thanks again, Marion [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] list: index of the element, that is TRUE
On Mon, Jan 16, 2012 at 10:15 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: 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) ) Or if you're writing a function: which(vapply(MyList, isTRUE, logical(1))) which will work even if length(MyList) == 0 Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-help@r-project.org 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.