[R] sample within groups-slight problem

David Winsemius dwinsemius at comcast.net
Wed Sep 7 02:38:18 CEST 2011


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



More information about the R-help mailing list