来自map()调用的data.frames连接列表 [英] Joining list of data.frames from map() call
本文介绍了来自map()调用的data.frames连接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在一种 tidyverse方式来连接data.frames列表(例如 full_join()
,但对于> 2个data.frames)?由于有 map()
的调用,我有一个data.frames列表。我以前曾使用 Reduce()
做类似的事情,但想将它们合并为管道的一部分-只是没有找到一种优雅的方法。玩具示例:
Is there a "tidyverse" way to join a list of data.frames (a la full_join()
, but for >2 data.frames)? I have a list of data.frames as a result of a call to map()
. I've used Reduce()
to do something like this before, but would like to merge them as part of a pipeline - just haven't found an elegant way to do that. Toy example:
library(tidyverse)
## Function to make a data.frame with an ID column and a random variable column with mean = df_mean
make.df <- function(df_mean){
data.frame(id = 1:50,
x = rnorm(n = 50, mean = df_mean))
}
## What I'd love:
my.dfs <- map(c(5, 10, 15), make.df) #%>%
# <<some magical function that will full_join() on a list of data frames?>>
## Gives me the result I want, but inelegant
my.dfs.joined <- full_join(my.dfs[[1]], my.dfs[[2]], by = 'id') %>%
full_join(my.dfs[[3]], by = 'id')
## Kind of what I want, but I want to merge, not bind
my.dfs.bound <- map(c(5, 10, 15), make.df) %>%
bind_cols()
推荐答案
我们可以使用 Reduce
set.seed(1453)
r1 <- map(c(5, 10, 15), make.df) %>%
Reduce(function(...) full_join(..., by = "id"), .)
或者这可以通过 reduce
library(purrr)
set.seed(1453)
r2 <- map(c(5, 10, 15), make.df) %>%
reduce(full_join, by = "id")
identical(r1, r2)
#[1] TRUE
这篇关于来自map()调用的data.frames连接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文