[R] sampling

David Winsemius dwinsemius at comcast.net
Thu Feb 17 21:40:23 CET 2011


On Feb 17, 2011, at 1:33 PM, andrija djurovic wrote:

> This is, maybe, not the best solution but I hope it will help you:
>
> x<-data.frame(id=c(1,1,1,2,2,2,2,3,3,3,4,4), v1=c(1:12), V2=c(12:23))
>
> do.call("rbind",by(x,x$id,function(x) x[c(sample(nrow(x),2)),]))
>
> Andrija
>

Another way (and note that by is just a wrppare for tapply):

 > tapply(1:nrow(x), x$id, sample, 2)
$`1`
[1] 2 3

$`2`
[1] 5 4

$`3`
[1] 10  8

$`4`
[1] 11 12

 > x[unlist( tapply(1:nrow(x), x$id, sample, 2) ), ]
    id v1 V2
2   1  2 13
3   1  3 14
5   2  5 16
6   2  6 17
9   3  9 20
8   3  8 19
12  4 12 23
11  4 11 22


> On Thu, Feb 17, 2011 at 6:39 PM, yf <chang648 at umn.edu> wrote:
>
>>
>> But i need for each id have two data.
>> Like...
>>> x
>>  id v1 V2
>> 1   1  1 12
>> 2   1  2 13
>>
>> 4   2  4 15
>> 5   2  5 16
>>
>>
>> 8   3  8 19
>> 9   3  9 20
>>
>> 11  4 11 22
>> 12  4 12 23
>>
>> So should write sample( if sample id >2  ,2). I don't know how to  
>> write
>> (if
>> sample id >2). Thanks.
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/sampling-tp3310184p3311253.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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list