[R] Sampling with conditions
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Tue Nov 8 18:59:51 CET 2011
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of SarahJoyes
> Sent: Tuesday, November 08, 2011 5:57 AM
> To: r-help at r-project.org
> Subject: Re: [R] Sampling with conditions
>
> That is exactly what I want, and it's so simple!
> Thanks so much!
>
Sarah,
I want to point out that my post was qualified by "something like". I am not sure it is exactly what you want. Since you didn't quote my post, let me show my suggestion and then express my concern.
n <- matrix(0,nrow=5, ncol=10)
repeat{
c1 <- sample(0:10, 4, replace=TRUE)
if(sum(c1) <= 10) break
}
n[,1] <- c(c1,10-sum(c1))
n
This nominally meets your criteria, but it will tend to result in larger digits being under-represented. For example, you unlikely to get a result like c(0,8,0,0,2) or (9,0,0,1,0).
That may be OK for your purposes, but I wanted to point it out.
You could use "something like"
n <- matrix(0,nrow=5, ncol=10)
c1 <- rep(0,4)
for(i in 1:4){
upper <- 10-sum(c1)
c1[i] <- sample(0:upper, 1, replace=TRUE)
if(sum(c1) == 10) break
}
n[,1] <- c(c1,10-sum(c1))
n
if that would suit your purposes better.
Good luck,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list