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
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
To find what names are common to both, use 'intersect'> x.bin.size <- list('3'=1, '4'=4, '5'=10) > y.bin.size <- list('2'=4, '3'=42, '4'=253, '5'=954) > sameNames <- intersect(names(x.bin.size), names(y.bin.size)) > sameNames[1] "3" "4" "5"> lapply(sameNames, function(x) sample(seq(y.bin.size[[x]]), x.bin.size[[x]]))[[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:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?