如何按R中的行随机组合不同的数据帧

How to combine different dataframes randomly by rows in R

我有数据帧 Relaxed.swimmingIntense.swimmingRestingBurst。它们共享列数(4 列),但行数不同。例如:

Relaxed.swimming <- data.frame(Behaviour= "Relaxed.swimming",
                               disurge=c(0.015,0.908,0.345,0.489),
                               diheave=c(0.398,0.782,0.198,0.634),
                               disway=c(0.491,0.398,0.189,0.592))

Intense.swimming <- data.frame(Behaviour= "Intense.swimming",
                               disurge=c(0.015,0.908,0.345),
                               diheave=c(0.398,0.782,0.198),
                               disway=c(0.491,0.398,0.189))


Burst <- data.frame(Behaviour= "Burst",
                    disurge=c(0.015,0.908),
                    diheave=c(0.398,0.782),
                    disway=c(0.491,0.398))

Resting <- data.frame(Behaviour= "Resting",
                      disurge=c(0.015,0.908,0.345),
                      diheave=c(0.398,0.782,0.198),
                      disway=c(0.491,0.398,0.189))

我只想按行合并它们(保留 4 列)。关键是我要组合成百上千次而且我想随机组合它们,也就是顺序不断变化(即rbind(Relaxed.swimming, Intense.swimming, Resting, Burst, Resting, Intense.swimming, Relaxed.swimming, Resting, etc))。虽然我想随机组合它们,但我想保持比例(四个向量的复制次数大致相同)。比率不必完全 1:1:1:1,但应该接近。

我想要这样的东西:

> df
          Behaviour disurge diheave disway
1           Resting   0.015   0.398  0.491
2           Resting   0.908   0.782  0.398
3           Resting   0.345   0.198  0.189
4             Burst   0.015   0.398  0.491
5             Burst   0.908   0.782  0.398
6  Intense.swimming   0.015   0.398  0.491
7  Intense.swimming   0.908   0.782  0.398
8  Intense.swimming   0.345   0.198  0.189
9  Relaxed.swimming   0.015   0.398  0.491
10 Relaxed.swimming   0.908   0.782  0.398
11 Relaxed.swimming   0.345   0.198  0.189
12 Relaxed.swimming   0.489   0.634  0.592
13            Burst   0.015   0.398  0.491
14            Burst   0.908   0.782  0.398
15 Relaxed.swimming   0.015   0.398  0.491
16 Relaxed.swimming   0.908   0.782  0.398
17 Relaxed.swimming   0.345   0.198  0.189
18 Relaxed.swimming   0.489   0.634  0.592
.          .            .       .      .
.          .            .       .      .
.          .            .       .      .

如何从上述 4 个数据帧的随机复制中获得一个大数据帧?

有人知道怎么做吗?

提前致谢

尝试这样做

library(tidyverse)
df_list <- list(Relaxed.swimming, Intense.swimming, Burst, Resting)

sample(df_list, 1, size = 10) %>% bind_rows()

如果比例不需要 100% 相同,那么这个 dplyr 解决方案应该可行:

首先将四个数据帧行绑定在一起:

library(dplyr)
All <- rbind(Relaxed.swimming, Intense.swimming, Burst, Resting)

然后将它们按Behavior分组并抽取任意大小的随机样本。随机样本通常保持内部比例不变:

All_s <- All %>% sample_n(1000, replace = T)

All_s[1:10,]
          Behaviour disurge diheave disway
1  Intense.swimming   0.015   0.398  0.491
2           Resting   0.345   0.198  0.189
3             Burst   0.345   0.198  0.189
4  Relaxed.swimming   0.345   0.198  0.189
5  Intense.swimming   0.489   0.634  0.592
6             Burst   0.345   0.198  0.189
7  Relaxed.swimming   0.345   0.198  0.189
8           Resting   0.489   0.634  0.592
9           Resting   0.015   0.398  0.491
10 Intense.swimming   0.241   0.241  0.241 

到目前为止的答案可能没有问题要求的那么多。从这个例子,期望的输出,看起来最终的结果应该有更多的洗牌例如,数据帧 Burst 有三行,但在例子输出中只有两行 Burst 彼此相邻。此函数复制数据帧列表,以随机顺序组合它们,然后可以选择将行再打乱一次。

random_replicate <- function(list_of_dataframes, n = 2, extra_shuffle = TRUE){
  n_frames <- length(list_of_dataframes)
  replicated <- replicate(n, do.call(rbind, sample(frames, n_frames)), simplify = FALSE)
  combined <- do.call(rbind, replicated)
  if (extra_shuffle) combined <- combined[sample.int(nrow(combined)),]
  return(combined)
}
list_of_dataframes <- list(Relaxed.swimming, Intense.swimming, Burst, Resting)

random_replicate(list_of_dataframes, 2)