Randomly Assign Integers in R within groups without replacement -
i running experiment 2 experiments: experiment_1 , experiment_2. each experiment has 5 different treatments (i.e. 1, 2, 3, 4, 5). trying randomly assign treatments within groups.
we via sampling without replacement iteratively within each group. want insure balanced sample possible in treatment (e.g. don't want end 4 subjects in group 1 getting assigned treatment 2 , no 1 getting treatment 1). if group has 23 subjects, want split respondent 4 subgroups of 5, , 1 subgroup of 3. want randomly sample without replacement across first subgroup of 5, gets assigned 1 of treatments, same things the second, third , 4th subgroup of 5, , final subgroup of 3 randomly sample without replacement. guarantee every treatment assigned @ least 4 subjects, , 3 assigned 5 subjects within group. groups in experiment , both treatments. resultant output this...
group experiment_1 experiment_2 [1,] 1 5 3 [2,] 1 3 2 [3,] 1 4 4 [4,] 1 1 5 [5,] 1 2 1 [6,] 1 2 3 [7,] 1 4 1 [8,] 1 3 2 [9,] 2 5 5 [10,] 2 1 4 [11,] 2 3 4 [12,] 2 1 5 [13,] 2 2 1 . . . . . . . . . . . .
i know how use sample
function, unsure how sample without replacement within each group, our output corresponds above described procedure. appreciated.
i think need shuffle sample ids, see example:
set.seed(124) #prepare groups , samples(shuffled) df <- data.frame(group=sort(rep(1:3,9)), sampleid=sample(1:27,27)) #treatments repeated nrow of df df$ex1 <- rep(c(1,2,3,4,5),ceiling(nrow(df)/5))[1:nrow(df)] df$ex2 <- rep(c(2,3,4,5,1),ceiling(nrow(df)/5))[1:nrow(df)] df <- df[ order(df$group,df$sampleid),] #check treatment distribution with(df,table(group,ex1)) # ex1 # group 1 2 3 4 5 # 1 2 2 2 2 1 # 2 2 2 2 1 2 # 3 2 2 1 2 2 with(df,table(group,ex2)) # ex2 # group 1 2 3 4 5 # 1 1 2 2 2 2 # 2 2 2 2 2 1 # 3 2 2 2 1 2
Comments
Post a Comment