# How do I sample N items from a list ensuring the samples are as similar as possible to each other?

Is there anyway to sample 6 numbers without replacement this list of 12 numbers

(490,   700,    850,    1220,   505,    650,    930,    810,    800,    960,    1130,   1480)


ensuring that the average is as close as possible to the remaining 6 numbers?

In this case is the average the best method of making sure that the two samples are as similar as possible? (Maybe something like a Density curve)?

• Perhaps you can expand on what you mean by "sample". If you want to draw simple random samples, then there is nothing you can do to guarantee anything (that's why it's random). However, if you have some type of purposive sampling protocol that you need to adhere to, this should set the criteria by which you can sample. So, I guess the real question is ¿what do you mean by "as similar as possible? – Gregg H Jul 2 '19 at 0:29
• @GreggH So these are resistance measurements of 12 wells of a cell line and we wanted to basically group 6 of the wells so that they as similar as possible to the other 6 wells in terms of the variation (is that the right word?) of the resistance measurements. – ahtmatrix Jul 2 '19 at 0:47
• Let me know if this is a correct summarization of what you are looking for: ¿how to split the data set so the variability within each group (say the standard deviation) is nearly equal? (Note: this isn't a question about the means of each of the set of 6, but how far each of the set of 6 is away from their respective sub-means.) – Gregg H Jul 2 '19 at 0:58
• @GreggH yes that sounds right – ahtmatrix Jul 2 '19 at 1:07
• If that's correct that's quite different from what I read your question to be asking. Are you sure? If you are, you need to edit your question to make that clear but either way, it appears your question needs to be clarified (via an edit). – Glen_b -Reinstate Monica Jul 2 '19 at 5:26

My first thought was that a hierarchical cluster analysis using a method that minimizes variability would be the best approach (e.g., Ward's method). However, this doesn't constrain to a solution with a fixed number of clusters (or more appropriately, a fixed number of items within a fixed number of clusters, such as a half-half split).

So, I would propose a less elegant sampling approach to find the "best" split sample with the desired criteria. Randomly sample different splits of the 12 values (into equal groups of 6 each); calculate the difference of the standard deviations for each group; retain the split that gives the lowest value. If you sample over a larger enough number of iterations, you should be able to find a reasonably good solution.

Here's the code I applied to your data:

vals <- c(490, 700, 850, 1220, 505, 650, 930, 810, 800, 960, 1130, 1480)

set.seed(1234)

min.abs.diff <- abs(diff(sd(vals[1:6]),sd(vals[7:12])))
best.split <- c(rep.int(0,6),rep.int(1,6))
for(ctr in 1:2000) {
grp <- sample(c(rep.int(0,6),rep.int(1,6)),12)
sd.diff <- abs(diff(aggregate(vals~grp,data.frame(vals,grp),sd)[,2]))
min.abs.diff <- min(min.abs.diff,sd.diff)
if(sd.diff == min.abs.diff && ctr>50) {
print(ctr)
print(grp)
best.split <- grp
}
}

aggregate(vals~grp,data.frame(vals,grp=best.split),sd)
vals[which(best.split==0)]
vals[which(best.split==1)]


This resulted in the following:

  grp     vals
1   0 283.7898
2   1 283.9674


And the groups were:

700  850  930  800 1130 1480
490 1220  505  650  810  960


I hope this helps with your project. And, I hope that if there is a more elegant way to cluster the data to achieve minimum differences in variability for fixed sample sizes...I'd very much like to see that.

• Thanks for your help! Would replacing sd with mean give me the groups with the min difference in averages? – ahtmatrix Jul 2 '19 at 3:15
• Yes, that would work. You also might want to consider searching for both smallest mean difference and smallest SD difference. – Gregg H Jul 2 '19 at 11:38