将列表中的数据框绑定到列表中的所有其他数据框

时间:2019-07-09 18:59:46

标签: r lapply rbind

有一些类似的问题,但不是我要的。我有此数据:

set.seed(34)
startingframe <-  data.frame(
  group1=factor(rep(c("a","b","c"),each=3,times=1)),
  time=rep(1:3,each=1,times=3),
  othercolumn=rnorm(1:9)
)
startingframe$control <- ifelse(startingframe$group1 == "c", 1, 0)
startingframe

out <- split(startingframe, startingframe$group1)
out

使用此数据,我想以编程方式将对照组(group1 = ccontrol = 1)绑定到两个治疗组,其输出完全如下所示:

list(
rbind(out[[1]], out[[3]]),
rbind(out[[2]], out[[3]]))

[[1]]
  group1 time othercolumn control
1      a    1  -0.1388900       0
2      a    2   1.1998129       0
3      a    3  -0.7477224       0
7      c    1   0.6706200       1
8      c    2  -0.8490146       1
9      c    3   1.0668045       1

[[2]]
  group1 time othercolumn control
4      b    1  -0.5752482       0
5      b    2  -0.2635815       0
6      b    3  -0.4554921       0
7      c    1   0.6706200       1
8      c    2  -0.8490146       1
9      c    3   1.0668045       1

理想情况下,我可以使用控制标志过滤器(control = 1)来执行此操作,而不是通过group1变量来显式执行此操作(以防治疗组数量发生变化)。

3 个答案:

答案 0 :(得分:2)

这是一种tidyverse的方法,尽管group_map是实验性的:

library(tidyverse)
control_group_df <- startingframe %>% filter(control == 1)

startingframe %>%
    filter(control != 1) %>%
    group_by(group1) %>%
    group_map(~ bind_rows(., control_group_df), keep = TRUE)

答案 1 :(得分:1)

基本R选项

# removing factors because in the vast majority of cases (incl this one) they make life harder
startingframe$group1 <- as.character(startingframe$group1)

control_split <- split(startingframe, startingframe$control)

lapply(split(control_split[['0']], control_split[['0']]$group1),
       rbind, control_split[['1']])



# $`a`
#   group1 time othercolumn control
# 1      a    1  -0.1388900       0
# 2      a    2   1.1998129       0
# 3      a    3  -0.7477224       0
# 7      c    1   0.6706200       1
# 8      c    2  -0.8490146       1
# 9      c    3   1.0668045       1
# 
# $b
#   group1 time othercolumn control
# 4      b    1  -0.5752482       0
# 5      b    2  -0.2635815       0
# 6      b    3  -0.4554921       0
# 7      c    1   0.6706200       1
# 8      c    2  -0.8490146       1
# 9      c    3   1.0668045       1

答案 2 :(得分:0)

为什么不list两个rbind

list(do.call(rbind, out[c(1, 3)]), do.call(rbind, out[c(2, 3)]))

或者完全避免使用out框架。

with(startingframe, list(
  rbind(startingframe[group1 == "a", ], startingframe[group1 == "c", ]),
  rbind(startingframe[group1 == "b", ], startingframe[group1 == "c", ])))

# [[1]]
#     group1 time othercolumn control
# a.1      a    1  -0.1388900       0
# a.2      a    2   1.1998129       0
# a.3      a    3  -0.7477224       0
# c.7      c    1   0.6706200       1
# c.8      c    2  -0.8490146       1
# c.9      c    3   1.0668045       1
# 
# [[2]]
#     group1 time othercolumn control
# b.4      b    1  -0.5752482       0
# b.5      b    2  -0.2635815       0
# b.6      b    3  -0.4554921       0
# c.7      c    1   0.6706200       1
# c.8      c    2  -0.8490146       1
# c.9      c    3   1.0668045       1