R中数据框的约束随机化 [英] Constrained Randomization of data frame in R

查看:67
本文介绍了R中数据框的约束随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框:

I have a data frame like this :

 Tem    Time
 10a      1
 10a      2
 10a      3 
 10a      4 
 20a      1
 20a      2
 20a      3
 20a      4 
 10b      1
 10b      2
 10b      3 
 10b      4 
 20b      1
 20b      2
 20b      3
 20b      4 

现在我想做随机化以减少这样的实验错误

Now I want to do randomization to reduce experimental errors like this

 Tem     Time
 10a      1
 10a      2
 10a      3 
 10a      4 
 20a      4 #( not the same sequence for Time in 10a)
 20a      2
 20a      3
 20a      1 
 #(for b, not same sequence for a in Tem)
 20b      3
 20b      1
 20b      2
 20b      4 
 10b      1 #(not the same sequence for Time in 20b)
 10b      3
 10b      2 
 10b      4 

最后,我需要将所有 12 个样本随机化在一起.我知道这很复杂.你能帮我做这件事或给我一些提示吗?提前致谢!

At the end, I need to randomize all the 12 samples together. I know it is very complicated. Could you help me to do this or give me some hints? Thanks in advance!

推荐答案

仅在 Tem 内随机化:

您可以使用 tidyverse 方法在组内随机采样(只有您再次对整个数据集进行采样):

Only randomize within Tem:

You can use tidyverse methods to randomly sample within groups (only you're sampling the whole dataset again):

library(tidyverse)
df <- data.frame(Tem = c("a", "b", "c"), Time = rep(1:4, 3)) %>% arrange(Tem, Time)

df %>% group_by(Tem) %>% sample_frac(1)
# A tibble: 12 x 2
# Groups:   Tem [3]
      Tem  Time
   <fctr> <int>
 1      a     4
 2      a     1
 3      a     2
 4      a     3
 5      b     4
 6      b     2
 7      b     3
 8      b     1
 9      c     3
10      c     2
11      c     4
12      c     1

如果你想随机化Tem以及Time

df %>% group_by(Tem) %>% sample_frac(1) %>% 
  split(df$Tem) %>% { .[sample(names(.), length(names(.)))] } %>%
  bind_rows()
# A tibble: 12 x 2
# Groups:   tem [3]
      tem  time
   <fctr> <int>
 1      c     2
 2      c     1
 3      c     4
 4      c     3
 5      b     4
 6      b     1
 7      b     2
 8      b     3
 9      a     1
10      a     2
11      a     4
12      a     3

(这个在 Tem 示例中与第一个相同,但随后将其拆分为由 Tem 定义的列表并重新采样名称,然后绑定全部重新组合在一起.(这可能不是最佳解决方案.)

(This one does the same within Tem sample as the first one, but then splits it into a list defined by Tem and re-samples the names, then binds it all back together. (This might not be the optimal solution.)

这篇关于R中数据框的约束随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆