Skip to content
Prev 276738 / 398506 Next

Sampling with conditions

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