[R] Subsetting a list of vectors

Hadley Wickham h.wickham at auckland.ac.nz
Mon Nov 10 01:43:07 CET 2003


I'm trying to subset a list which contains variable length vectors.  
What I want to do is extract (eg.) the 3rd item in each vector (with 
length >= 3).  At the moment I'm using sapply(list.of.vectors, 
function(x) {x[3]}).  The problem with this is that sapply returns a 
list of the same length of list.of.vectors so I end up with a whole lot 
of null entries from those vectors that aren't long enough.  I have a 
similar problem if I want to select all the vectors where the 3rd item 
is a specified value.

Does anyone have any better solutions?

Thanks for you help,


More information about the R-help mailing list