Rbind 列表中的数据框到列表中的所有其他数据框
Rbind dataframe in list to all other dataframes in list
有几个类似的问题已发布,但不是我要找的。我有这个数据:
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 = c
或 control = 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 变量明确地执行此操作(以防治疗组的数量发生变化)。
这是一个 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)
为什么不只是 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
基本 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
有几个类似的问题已发布,但不是我要找的。我有这个数据:
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 = c
或 control = 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 变量明确地执行此操作(以防治疗组的数量发生变化)。
这是一个 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)
为什么不只是 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
基本 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