Hi, For my analytical work, I need to draw a sample of certain sample size from a denied population, where population members are marked by non-negative integers, such that sum of sample members if fixed. For example, Population = 0:100 Sample_size = 10 Sample_Sum = 20 Under this setup if my sample members are X1, X2, ..., X10 then I should have X1+X2+...+X10 = 20 Sample drawing scheme may be with/without replacement Is there any R function to achieve this? One possibility is to employ naive trial-error approach, but this doesnt seem to be practical as it would take long time to get the final sample with desired properties. Any pointer would be greatly appreciated.
1) extract relevant observations to a new data frame. Either of these two commands works. filter(df, age > 24) df[df$age > 24, ] 2) sample the new data frame df[sample(nrow(df), 10, replace = TRUE), ] change TRUE to FALSE if you do not want replacement. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Brian Smith Sent: Monday, April 14, 2025 7:27 AM To: r-help at r-project.org Subject: [R] Drawing a sample based on certain condition [External Email] Hi, For my analytical work, I need to draw a sample of certain sample size from a denied population, where population members are marked by non-negative integers, such that sum of sample members if fixed. For example, Population = 0:100 Sample_size = 10 Sample_Sum = 20 Under this setup if my sample members are X1, X2, ..., X10 then I should have X1+X2+...+X10 = 20 Sample drawing scheme may be with/without replacement Is there any R function to achieve this? One possibility is to employ naive trial-error approach, but this doesnt seem to be practical as it would take long time to get the final sample with desired properties. Any pointer would be greatly appreciated. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
?s 12:26 de 14/04/2025, Brian Smith escreveu:> Hi, > > For my analytical work, I need to draw a sample of certain sample size > from a denied population, where population members are marked by > non-negative integers, such that sum of sample members if fixed. For > example, > > Population = 0:100 > Sample_size = 10 > Sample_Sum = 20 > > Under this setup if my sample members are X1, X2, ..., X10 then I > should have X1+X2+...+X10 = 20 > > Sample drawing scheme may be with/without replacement > > Is there any R function to achieve this? One possibility is to employ > naive trial-error approach, but this doesnt seem to be practical as it > would take long time to get the final sample with desired properties. > > Any pointer would be greatly appreciated. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Use the multinomial distribution. The problem with this is that the population is not part of the solution. But the sum is Sample_Sum. Population <- 0:100 Sample_size <- 10 Sample_Sum <- 20 probs <- rep(1/Sample_size, Sample_size) # one sample rmultinom(1, Sample_Sum, prob = probs) # five samples rmultinom(5, Sample_Sum, prob = probs) Another way, found on StackOverflow. # user: https://stackoverflow.com/users/4996248/john-coleman # answer: https://stackoverflow.com/a/49016614/8245406 rand.nums <- function(a,b,n,k){ #finds n random integers in range a:b which sum to k while(TRUE){ x <- sample(1:(k - n*a), n - 1, replace = TRUE) #cutpoints x <- sort(x) x <- c(x, k - n*a) - c(0, x) if(max(x) <= b - a) return(a + x) } } rand.nums(0, 100, Sample_size, Sample_Sum) Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
On 2025-04-14 7:26 a.m., Brian Smith wrote:> Hi, > > For my analytical work, I need to draw a sample of certain sample size > from a denied population, where population members are marked by > non-negative integers, such that sum of sample members if fixed. For > example, > > Population = 0:100 > Sample_size = 10 > Sample_Sum = 20 > > Under this setup if my sample members are X1, X2, ..., X10 then I > should have X1+X2+...+X10 = 20 > > Sample drawing scheme may be with/without replacement > > Is there any R function to achieve this? One possibility is to employ > naive trial-error approach, but this doesnt seem to be practical as it > would take long time to get the final sample with desired properties. > > Any pointer would be greatly appreciated.One general way to think of this problem is that you are defining a distribution on the space of all possible samples of size 10, such that the probability of a sample is X if the sum is 20, and zero otherwise, and you want to sample from this distribution. There's probably a slick method to do that for your example, but if you've got a general population instead of that special one, I doubt it. What I would do is the following: Define another distribution on samples that has probabilities that depend on the sum of the sample, with the highest probabilities attached to ones with the correct sum, and probabilities for other sums declining with distance from the sum. For example, maybe P(sum) = Y/(1 + abs(sum - 20)) for some constant Y. You can use MCMC to sample from that distribution and then only keep the samples where the sum is exactly equal to the target sum. If you do that, you don't need to care about the value of Y. but you do need to think about how proposed moves are made, and you probably need to use a different function than the example above for acceptable efficiency. Duncan Murdoch