[R] sample within groups-slight problem

Jack Siegrist jacksie at eden.rutgers.edu
Wed Sep 7 02:13:30 CEST 2011


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.



More information about the R-help mailing list