简体   繁体   English

使用带有 R 条件的 sample()

[英]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.有 7 种治疗方法,我需要确保一个人在被随机分配时不会多次接受相同的治疗。 There are 35 individuals and 7 treatments so there are 5 replicates for each treatment.有 35 个人和 7 个处理,因此每个处理有 5 个重复。

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?有没有办法对 sample() 施加一个条件,以使个体 x 不能得到与先前试验相同的处理?

You seem to want to first randomly assign individuals three treatments, so if there are K treatments, and you want to randomly pick 3 without replacement, so do that for each individual, and then merge in the treatment effects.您似乎想首先随机分配个人三个治疗,所以如果有 K 个治疗,并且您想随机选择 3 个而不替换,那么对每个人都这样做,然后合并治疗效果。 For example, using your numbers, and using data.table , here's a solution:例如,使用您的号码并使用data.table ,这是一个解决方案:

set.seed(566)
library(data.table)

exp_num = 7
#set up a data.table to hold treatment effects
treat_dt = data.table("experiment_num" = 1:exp_num, "treatment_effect" = c(2,4,8,16,32,64,100))

#now create a datatable of subjectsXtrials
subj_dt = data.table(expand.grid("id" = 1:35, "trial" = paste0("trial",1:3)))

#now randomly assign three experiments without replacement by id
subj_dt[, exp_assigned := sample(1:exp_num,3, replace = F), by = id]

#now merge in effects with treat_dt by experiment...
subj_dt = merge(subj_dt,treat_dt, by.x = "exp_assigned",by.y = "experiment_num", all.x = T, all.y = F)

#and youre done! option to get back a dataset where each id is a single row
alt_dt = dcast(subj_dt[,.(id,trial,treatment_effect)], id ~ trial, value.var = "treatment_effect")

Then the output looks as follows for alt_dt然后 output 看起来如下alt_dt

> head(alt_dt)
   id trial1 trial2 trial3
1:  1    100     32      8
2:  2    100     64     32
3:  3      4     16      2
4:  4    100     64      8
5:  5      8     16      4
6:  6     64    100      8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM