I want to sample within groups, and when a group has only one associated number to just return that number. If I use this code: groups <- c(1, 2, 2, 2, 3) numbers <- 1:5 tapply(numbers, groups, FUN = sample) I get the following output:> groups <- c(1, 2, 2, 2, 3) > numbers <- 1:5 > tapply(numbers, groups, FUN = sample)$`1` [1] 1 $`2` [1] 3 2 4 $`3` [1] 2 3 5 1 4 Can someone tell me why the $'3' result samples all of the numbers and how to prevent it from doing so? I want the output for the $'3' part to just be 5 in this example. Thanks for your help.> sessionInfo()R version 2.11.1 (2010-05-31) i386-pc-mingw32 -- View this message in context: http://r.789695.n4.nabble.com/sample-within-groups-slight-problem-tp3794912p3794912.html Sent from the R help mailing list archive at Nabble.com.
Hi there, in the third case you get sample(5) which is exactly what you asked for. from ?sample: "If x has length 1, is numeric (in the sense of is.numeric) and x >1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x). See the examples. " 2011/9/6 Jack Siegrist <jacksie at eden.rutgers.edu>:> I want to sample within groups, and when a group has only one associated > number to just return that number. > > If I use this code: > > groups <- c(1, 2, 2, 2, 3) > numbers <- 1:5 > tapply(numbers, groups, FUN = sample) > > I get the following output: > >> ?groups <- c(1, 2, 2, 2, 3) >> ?numbers <- 1:5 >> ?tapply(numbers, groups, FUN = sample) > $`1` > [1] 1 > > $`2` > [1] 3 2 4 > > $`3` > [1] 2 3 5 1 4 > > Can someone tell me why the $'3' result samples all of the numbers and how > to prevent it from doing so? I want the output for the $'3' part to just be > 5 in this example. > > Thanks for your help. > > >> sessionInfo() > R version 2.11.1 (2010-05-31) > i386-pc-mingw32 > > -- > View this message in context: http://r.789695.n4.nabble.com/sample-within-groups-slight-problem-tp3794912p3794912.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >
On Sep 6, 2011, at 8:13 PM, Jack Siegrist wrote:> I want to sample within groups, and when a group has only one > associated > number to just return that number.And what we supposed to do when it has more than one value?????> > If I use this code: > > groups <- c(1, 2, 2, 2, 3) > numbers <- 1:5 > tapply(numbers, groups, FUN = sample) > > I get the following output: > >> groups <- c(1, 2, 2, 2, 3) >> numbers <- 1:5 >> tapply(numbers, groups, FUN = sample) > $`1` > [1] 1 > > $`2` > [1] 3 2 4 > > $`3` > [1] 2 3 5 1 4 > > Can someone tell me why the $'3' result samples all of the numbers > and how > to prevent it from doing so? I want the output for the $'3' part to > just be > 5 in this example.Type: sample(5) If typing it once, does not lead you to enlightenment, then continue typing it until enlightenment is achieved. -- David "Buddha" Winsemius
2011/9/6 Jean-Christophe BOU?TT? <jcbouette at gmail.com>: you could tapply function(x) if(length(x)==1) x else sample(x) or something like this> > 2011/9/6 Jean-Christophe BOU?TT? <jcbouette at gmail.com>: >> Hi there, >> in the third case you get sample(5) which is exactly what you asked for. >> >> from ?sample: >> "If x has length 1, is numeric (in the sense of is.numeric) and x >>> 1, sampling via sample takes place from 1:x. Note that this >> convenience feature may lead to undesired behaviour when x is of >> varying length in calls such as sample(x). See the examples. " >> >> 2011/9/6 Jack Siegrist <jacksie at eden.rutgers.edu>: >>> I want to sample within groups, and when a group has only one associated >>> number to just return that number. >>> >>> If I use this code: >>> >>> groups <- c(1, 2, 2, 2, 3) >>> numbers <- 1:5 >>> tapply(numbers, groups, FUN = sample) >>> >>> I get the following output: >>> >>>> ?groups <- c(1, 2, 2, 2, 3) >>>> ?numbers <- 1:5 >>>> ?tapply(numbers, groups, FUN = sample) >>> $`1` >>> [1] 1 >>> >>> $`2` >>> [1] 3 2 4 >>> >>> $`3` >>> [1] 2 3 5 1 4 >>> >>> Can someone tell me why the $'3' result samples all of the numbers and how >>> to prevent it from doing so? I want the output for the $'3' part to just be >>> 5 in this example. >>> >>> Thanks for your help. >>> >>> >>>> sessionInfo() >>> R version 2.11.1 (2010-05-31) >>> i386-pc-mingw32 >>> >>> -- >>> View this message in context: http://r.789695.n4.nabble.com/sample-within-groups-slight-problem-tp3794912p3794912.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> ______________________________________________ >>> R-help at r-project.org 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. >>> >> >
On 11-09-06 8:13 PM, Jack Siegrist wrote:> I want to sample within groups, and when a group has only one associated > number to just return that number. > > If I use this code: > > groups<- c(1, 2, 2, 2, 3) > numbers<- 1:5 > tapply(numbers, groups, FUN = sample) > > I get the following output: > >> groups<- c(1, 2, 2, 2, 3) >> numbers<- 1:5 >> tapply(numbers, groups, FUN = sample) > $`1` > [1] 1 > > $`2` > [1] 3 2 4 > > $`3` > [1] 2 3 5 1 4 > > Can someone tell me why the $'3' result samples all of the numbers and how > to prevent it from doing so? I want the output for the $'3' part to just be > 5 in this example.Because sample(5) gives a permutation of the numbers 1:5. See the last example in ?sample for a way to avoid this "feature". Duncan Murdoch> > Thanks for your help. > > >> sessionInfo() > R version 2.11.1 (2010-05-31) > i386-pc-mingw32 > > -- > View this message in context: http://r.789695.n4.nabble.com/sample-within-groups-slight-problem-tp3794912p3794912.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.