gpt4 book ai didi

r - 列表列数据框中的 purrr pmap

转载 作者:行者123 更新时间:2023-12-04 09:40:19 25 4
gpt4 key购买 nike

当我有一个包含两个输入列(a 和 b)的列表列数据框 (df1) 时,我可以使用 map2 获取一个新列

d1 <- mtcars %>% slice(1:10) %>% group_by(cyl) %>% nest(.key = "a")
d2 <- mtcars %>% slice(11:20) %>% group_by(cyl) %>% nest(.key = "b")
df1 <- d1 %>% left_join(d2)

my_fun <- function(a, b) {
# some manipulations that involve the dataframe a and b and result in
# another dataframe
# below, just a trivial manipulation
a %>% bind_rows(b)
}

df1 %>% mutate(z = map2(a, b, my_fun))

现在假设我有一个带有 3 个输入列(a、b 和 c)的新数据框 (df2)。
d3 <- mtcars %>% slice(21:10) %>% group_by(cyl) %>% nest(.key = "c")
df2 <- d1 %>% left_join(d2) %>% left_join(d3)

如何使用 3 个输入列获取新列?由于没有 map3,我想我应该使用 pmap,但我不知道如何使用。

最佳答案

正如@Sathish 评论的那样pmap可以使用,并且可以在 mutate 内使用通过将列放在 list 中然后应用函数

library(tidyverse)
df2 %>%
mutate(z = pmap(list(a, b, c), bind_rows))

关于r - 列表列数据框中的 purrr pmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50949422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com