[英]Merging list-column data frames
I have a data frame / tibble with several list-columns that I want to merge / join with each other, but I'm struggling to find an elegant way to do so using the mutate(map())
workflow.我有一个包含几个列表列的数据框/小标题,我想将它们相互合并/连接,但我正在努力寻找一种使用mutate(map())
工作流程的优雅方法。
Suppose I have a data frame with the following structure:假设我有一个具有以下结构的数据框:
library(gapminder)
# Setup
gap_1 <- gapminder %>%
select(-pop, -gdpPercap) %>%
group_by(country, continent) %>%
nest() %>%
rename(lifeExp = data)
gap_2 <- gapminder %>%
select(-lifeExp, -gdpPercap) %>%
group_by(country, continent) %>%
nest() %>%
rename(pop = data)
gap_3 <- gapminder %>%
select(-lifeExp, -pop) %>%
group_by(country, continent) %>%
nest() %>%
rename(gdpPercap = data)
# What my data looks like
gap_main <- reduce(list(gap_1, gap_2, gap_3), left_join, by = c("country", "continent")) %>% ungroup()
What I'm hoping to do is to merge the list-columns lifeExp
, pop
, and gdpPercap
by year (There are more than three such columns in my data).我希望做的是按年合并列表列lifeExp
、 pop
和gdpPercap
(我的数据中有超过三个这样的列)。 But I'm not sure how one would go about this using mutate
and pmap
/ map2
.但我不确定 go 如何使用mutate
和pmap
/ map2
来解决这个问题。
My attempts so far have been to use pmap
with bind_cols
like so: (this is assuming rows correspond)到目前为止,我的尝试是像这样将pmap
与bind_cols
一起使用:(这是假设行对应)
gap_main %>%
mutate(all = pmap(list(lifeExp, pop, gdpPercap), ~ bind_cols(...)))
or repeatedly use map2
with left_join
.或重复使用map2
和left_join
。 Is there a more elegant way of doing this perhaps using reduce?有没有更优雅的方法可以使用reduce?
I am not sure how your desired output should look like, would the approach below work for you?我不确定您想要的 output 应该是什么样子,下面的方法对您有用吗? It uses {dplyr}'s rowwise
notation and then basically purrr::reduce
to join all three columns in one call.它使用 {dplyr} 的rowwise
表示法,然后基本上purrr::reduce
在一次调用中加入所有三列。
library(tidyverse)
library(gapminder)
gap_main %>%
rowwise %>%
mutate(data = list(reduce(list(lifeExp, pop, gdpPercap), left_join, by = "year")))
#> # A tibble: 142 x 6
#> # Rowwise:
#> country continent lifeExp pop gdpPercap data
#> <fct> <fct> <list> <list> <list> <list>
#> 1 Afghanist… Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 2 Albania Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 3 Algeria Africa <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 4 Angola Africa <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 5 Argentina Americas <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 6 Australia Oceania <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 7 Austria Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 8 Bahrain Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 9 Bangladesh Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 10 Belgium Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> # … with 132 more rows
Alternatively you could use pmap
instead of rowwise
in the following way:或者,您可以通过以下方式使用pmap
而不是rowwise
:
gap_main %>%
mutate(all = pmap(list(lifeExp, pop, gdpPercap),
~ reduce(list(..1, ..2, ..3), left_join, by = "year")))
#> # A tibble: 142 x 6
#> country continent lifeExp pop gdpPercap all
#> <fct> <fct> <list> <list> <list> <list>
#> 1 Afghanist… Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 2 Albania Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 3 Algeria Africa <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 4 Angola Africa <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 5 Argentina Americas <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 6 Australia Oceania <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 7 Austria Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 8 Bahrain Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 9 Bangladesh Asia <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> 10 Belgium Europe <tibble [12 ×… <tibble [12 … <tibble [12 ×… <tibble [12…
#> # … with 132 more rows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.