[R] Subsetting a list of vectors

Gabor Grothendieck ggrothendieck at myway.com
Sat Nov 15 02:40:29 CET 2003



One idea.  If sapply were to have an rm= parameter then the 
4 solutions below would reduce to:

   sapply( v, "[", 3, rm=NA )
   sapply( v, function(x) if (length(x)>=3) x[3], rm=NULL )

   sapply( 1:10, function(x) if (x%%2==0) x^2, rm=NULL )
   sapply( 1:10, function(x) if(x%%2==0)x^2 else NA, rm=NA )

---
 
Date: Mon, 10 Nov 2003 00:23:17 -0500 (EST) 
From: Gabor Grothendieck <ggrothendieck at myway.com>
To: <edd at debian.org>, <ray at mcs.vuw.ac.nz>, <h.wickham at auckland.ac.nz> 
Cc: <r-help at stat.math.ethz.ch> 
Subject: Re: [R] Subsetting a list of vectors 

 
 


Dirk and Ray have provided two very clever solutions which 
perform transformation and selection in one go 
by returning NA and NULL respectively for unwanted elements 
and then eliminating the NAs and NULLs. 

I thought it would be worthwhile to bring them together and 
make some further minor improvements.

Note that for the NULL solution we use the fact that 
if(FALSE)... with no else leg equals NULL.


Problem 1. If v is a list of vectors, get the vector which is the
third element of each vector in v. Do not include any elements 
for vectors with less than 3 elements.

Here the NA solution is particularly short:

as.numeric( na.omit( sapply( v, "[", 3 ) ) ) 

but the NULL solution seems closer to the list comprehension idea:

unlist( sapply( v, function(x) if (length(x)>=3) x[3] ) )


Problem 2. Express this Python program in R:
# give me the squares of the even numbers from 1-10, in a list. 
>>> [ x*x for x in range(1,11) if x%2 == 0]


Here the NULL Solution is both short and closer to the Python one:

unlist( sapply( 1:10, function(x) if (x%%2==0) x^2 ) )

while the NA solution is:

as.numeric(na.omit(sapply(1:10,function(x)if(x%%2==0)x^2 else NA)))




 --- On Mon 11/10, Gabor Grothendieck < ggrothendieck at myway.com > wrote:
From: Gabor Grothendieck [mailto: ggrothendieck at myway.com]
To: edd at debian.org, ray at mcs.vuw.ac.nz, h.wickham at auckland.ac.nz
     Cc: r-help at stat.math.ethz.ch
Date: Mon, 10 Nov 2003 00:23:17 -0500 (EST)
Subject: Re: [R] Subsetting a list of vectors

<br><br>Dirk and Ray have provided two very clever solutions which <br>perform transformation and selection in one go <br>by returning NA and NULL respectively for unwanted elements <br>and then eliminating the NAs and NULLs.  <br><br>I thought it would be worthwhile to bring them together and <br>make some further minor improvements.<br><br>Note that for the NULL solution we use the fact that <br>if(FALSE)... with no else leg equals NULL.<br><br><br>Problem 1. If v is a list of vectors, get the vector which is the<br>third element of each vector in v.  Do not include any elements <br>for vectors with less than 3 elements.<br><br>Here the NA solution is particularly short:<br><br>  as.numeric( na.omit( sapply( v, "[", 3 ) ) ) <br><br>but the NULL solution seems closer to the list comprehension idea:<br><br>  unlist( sapply( v, function(x) if (length(x)>=3) x[3] ) )<br><br><br>Problem 2. Express this Python program in R:<br>     # give me the squares of the even numbers from 1-10, in a list. <br>     >>> [ x*x for x in range(1,11) if x%2 == 0]<br><br><br>Here the NULL Solution is both short and closer to the Python one:<br><br>  unlist( sapply( 1:10, function(x) if (x%%2==0) x^2 ) )<br><br>while the NA solution is:<br><br>  as.numeric(na.omit(sapply(1:10,function(x)if(x%%2==0)x^2 else NA)))<br><br>______________________________________________<br>R-help at stat.math.ethz.ch mailing list<br>https://www.stat.math.ethz.ch/mailman/listinfo/r-help<br>




More information about the R-help mailing list