[R] Iterative sampling with restrictions

PIKAL Petr petr.pikal at precheza.cz
Tue Aug 21 09:39:42 CEST 2012


Hi

why not shuffle your values by

x <- 1:60
x <- sample(x, 60)

and then add groups
groups <- rep(paste("g",1:3, sep=""), each=20)

If you want you can suffle also groups.

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of David A. Kim
> Sent: Tuesday, August 21, 2012 6:17 AM
> To: r-help at r-project.org
> Subject: [R] Iterative sampling with restrictions
> 
> Hi all,
> 
> I'm working on a seemingly trivial problem that I can't figure out how
> to implement in R.  I'd be most grateful for any help here.
> 
> I want to do the following: first, randomly assign each of n units to
> one of g groups of size n/g. Then, randomly re-assign each of the n
> units to a different group (i.e., same as the first step, but the unit
> can't be assigned to a group to which it's already belonged).  Then
> repeat this step until each unit has at some point been assigned to
> every group.
> 
> More concretely, say I have 60 units and 3 groups into which to divide
> the units.  I could first do something like:
> 
> group1<-sample(1:60,20)
> group2<-sample(setdiff(1:60,group1),20)
> group3<-sample(setdiff(1:60,c(group1,group2)),20)
> 
> But then how to randomly re-assign group membership such that all units
> are assured a different group assignment in the second "wave" of
> grouping?  Just narrowing the sampling pool to those units that weren't
> previously assigned to a given group won't work (consider the case
> where groups 1 and 2 swap units: in the second wave, there would be no
> units to assign to group 3 as all the remaining units had already been
> in group 3 in the first wave).
> 
> Most grateful for any assistance,
> 
> David
> 
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list