Skip to content

how to loop through 2 lists with different indexes

2 messages · Tania Oh, jim holtman

#
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
#
To find what names are common to both, use 'intersect'
[1] "3" "4" "5"
[[1]]
[1] 12

[[2]]
[1]  95 145 228  51

[[3]]
 [1] 858 901 630 599  59 196 168 651 364 728

        
On 9/6/06, Tania Oh <tania.oh at brasenose.oxford.ac.uk> wrote: