mardi 5 mai 2020

Use sample() with conditions in R

I have a dataset I created to randomly assign treatments to experimental subjects. Individuals will be subjected to a treatment thrice. There are 7 treatments and I need to make sure that a single individual does not receive the same treatment more than once while still being randomly assigned. There are 35 individuals and 7 treatments so there are 5 replicates for each treatment.

the data:

set.seed(566)
treatments<-rep(c(2,4,8,16,32,64,100), each=5)
random_design<-data.frame(individual=c(1:35), trial1=sample(treatments), trial2=sample(treatments), trial3=sample(treatments))

As you can see, some individuals are subjected to the same treatment in different trials. Is there a way to impose a condition to sample(), so that individual x cannot have the same treatment than in a previous trial?




Aucun commentaire:

Enregistrer un commentaire