The third phase is determined since there are only three groups, but I
should have added them to group specifications to make things clearer:

> require(e1071)
> groups <- apply(permutations(3), 1, function(x) paste(x, collapse="/"))
> assign <- sample(rep(groups, 10))
> table(assign)
assign
1/2/3 1/3/2 2/1/3 2/3/1 3/1/2 3/2/1 
   10    10    10    10    10    10 

-------
David

> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of David L Carlson
> Sent: Wednesday, August 22, 2012 5:11 PM
> To: 'Daniel Nordlund'; r-help@r-project.org
> Subject: Re: [R] Iterative sampling with restrictions
> 
> If I understand you, this is just random assignment over 6 groups: 1/2,
> 1/3,
> 2/1, 2/3, 3/1, 3/2.
> 
> > groups <- c("1/2", "1/3", "2/1", "2/3", "3/1", "3/2")
> > assign <- sample(rep(groups, 10))
> > table(assign)
> assign
> 1/2 1/3 2/1 2/3 3/1 3/2
>  10  10  10  10  10  10
> 
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
> 
> > -----Original Message-----
> > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Daniel Nordlund
> > Sent: Tuesday, August 21, 2012 2:58 AM
> > To: r-help@r-project.org
> > Subject: Re: [R] Iterative sampling with restrictions
> >
> > > -----Original Message-----
> > > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> > project.org]
> > > On Behalf Of David A. Kim
> > > Sent: Monday, August 20, 2012 9:17 PM
> > > To: r-help@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
> > >
> >
> > David,
> >
> > I would collect the sample waves into a data.frame.  I am sure
> someone
> > will be able to help you with a more general and/or efficient
> solution,
> > but to get you started I have provided one possible solution to your
> 60
> > unit 3 wave example
> >
> > #create data.frame with IDs
> > df <- data.frame(id=1:60)
> >
> > #create first sample wave
> > df$wave1 <- sample(rep(1:3,20))
> >
> > #reorder df and create second wave sample
> > df <- df[order(df$wave1),]
> > df$wave2 <-  c(sample(rep(c(2,3),10)),
> >                sample(rep(c(1,3),10)),sample(rep(c(1,2),10)))
> >
> > #now use set diff to create 3rd wave
> > for(i in 1:60) df[i,'wave3'] <- unlist(setdiff(1:3,df[i,2:3]))
> >
> > df
> >
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > 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.

______________________________________________
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.

Reply via email to