[R] how to loop through 2 lists with different indexes

Tania Oh tania.oh at brasenose.oxford.ac.uk
Wed Sep 6 17:03:34 CEST 2006


Very sorry if this mail is sent out twice to the list, I wasn't sure  
if the email address I used in the first go was correct. 	



	From: 	  tania.oh at bnc.ox.ac.uk
	Subject: 	how to loop through 2 lists with different indexes
	Date: 	6 September 2006 15:16:21 BDT
	To: 	  r-help at lists.R-project.org

Dear all,

I am a newbie in R and need some help please.  (I do apologise if my  
email is not as informative as it should be, I've tried to include  
the relevant details without overcrowding it with the rest of the code)

I would like to sample (without replacement) Y objects based on the  
number of objects in X in 5 different bins. I'm having trouble  
because the list object in which the number of objects of X is stored  
in doesn't start its index from 0.

# number of X objects in each of the 5 bins
   x.bin.size <- lapply(x.by.bins, nrow)

x.bin.size
$`3`
[1] 1

$`4`
[1] 3

$`5`
[1] 10


# no. of objects in each of the 5 bins of Y
  y.bin.size <- lapply(y.by.bins, nrow)

 >   y.bin.size
$`2`
[1] 4

$`3`
[1] 42

$`4`
[1] 253

$`5`
[1] 945

how do I loop through Y and sample from X when the index of Y starts  
from 2 and that of X starts from 3? in X, the missing index $`2` is  
assumed to have 0 objects in it. hence, I would only sample from Y  
and X starting from index 3.

sample (y.bin.size$`3`, x.bin.size$`3`, replace=FALSE)

but how should I do this in an R command without knowing which  
indexes (of X ) are empty? Any pointers would be greatly appreciated.


Many thanks in advance,
tania

  ---
Tania Oh
D.Phil student
Department of Physiology, Anatomy and Genetics
University of Oxford
OX1 3TU



More information about the R-help mailing list