purrr pmap在列表列数据框中
当我有一个包含两个输入列(a和b)的列表列数据帧(df1)时,我可以使用map2获得一个新列purrr pmap在列表列数据框中,r,tidyverse,purrr,pmap,R,Tidyverse,Purrr,Pmap,当我有一个包含两个输入列(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
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))
d1%slice(1:10)%%>%groupby(cyl)%%>%nest(.key=“a”)
d2%切片(11:20)%%>%group_by(cyl)%%>%nest(.key=“b”)
df1%左联合(d2)
我的乐趣%bind_行(b)
}
df1%>%突变(z=map2(a,b,我的乐趣))
现在假设我有一个新的数据帧(df2),它有3个输入列(a、b和c)
d3%slice(21:10)%%>%groupby(cyl)%%>%nest(.key=“c”)
df2%左接缝(d2)%>%左接缝(d3)
如何使用3个输入列获取新列?由于没有map3,我想我应该使用pmap,但我不知道如何使用。正如@Sathish评论的那样,可以使用
pmap
,这可以在mutate
中使用,方法是将列放在列表中,然后应用该函数
library(tidyverse)
df2 %>%
mutate(z = pmap(list(a, b, c), bind_rows))
pmap(df[,1:3],我的乐趣)
。函数必须接受三个或更多参数。请尝试帮助页面中的示例?pmap
trydf2%>%mutate(z=pmap(列表(a、b、c)、bind_行))
整天都在想你:D@andrelrico ha
library(tidyverse)
df2 %>%
mutate(z = pmap(list(a, b, c), bind_rows))