且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何按因子生成随机处理变量?

更新时间:2023-02-26 18:47:47

我假设您只想分配一个 1级处理和一个 2级处理每个班级.您可以使用 plyr 包中的 ddply 函数轻松完成:

I'll assume you just want to assign one level 1 treatment and one level 2 treatment in each class. You can use the ddply function from the plyr package to do it easily:

  set.seed(1)
  require(plyr)
> ddply(x, .(class), transform, 
        treat = replace(treat, sample(seq_along(treat),2), 1:2))

   ID class treat
1   a     1     0
2   b     1     1
3   c     1     0
4   d     1     0
5   e     1     2
6   f     2     0
7   g     2     0
8   h     2     1
9   i     2     2
10  j     2     0

解释一下:ddply 函数通过 class 变量分割数据帧,并且在每个数据帧内,它transforms" treat 列将 2 个随机选择的条目替换为 1 和 2.sample(...,2) 函数在 treat 列.其他变体(例如,为每种处理类型分配 1 个以上)可以类似地进行.

To explain: the ddply function splits the data-frame by the class variable, and within each data-frame, it "transforms" the treat column by replacing 2 randomly chosen entries by 1 and 2. The sample(...,2) function picks two random indices in the treat column. Other variants (e.g. assign more than 1 of each treatment type) can be done similarly.