[R] subscripts in lists

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 12 10:19:39 CEST 2003


On Tue, 12 Aug 2003, Peter Wolf wrote:

> What about ...
> 
>  > unlist(lis)[which(unlist(lis)=="next")+1]
> [1] "want1" "want2"
> 
> ... to avoid the loop in sapply?

That has a loop in each unlist, of course.  (A slicker version of that
solution was posted earlier by Richard.)

Note that you have to loop over the list elements to do this: both sapply
(really lapply) and unlist do that in C code.  It's not even clear to me
that it is worth avoiding looping in R code: for() loops in R are pretty
efficient, and the collective memory of problems in S+3.x (x < 4) is still
affecting decisions today.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list