In addition to Dan's quite valid concern, the final sample is not truly 'random' - the first k - 1 elements are randomly chosen, but the last is determined so that the constraint is met.
Dennis On Tue, Nov 8, 2011 at 9:59 AM, Nordlund, Dan (DSHS/RDA) <nord...@dshs.wa.gov> wrote: >> -----Original Message----- >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- >> project.org] On Behalf Of SarahJoyes >> Sent: Tuesday, November 08, 2011 5:57 AM >> To: r-help@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 > > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.