[英]Error when using dplyr::mutate with mapply
I want to use mutate on each of two data frames in a list, adding a column z = 3
to the first and z = 4
to the second (and returning a list of two data frames). 我想在列表中的两个数据帧中的每一个上使用mutate,将第z = 3
列添加到第一个,将z = 4
到第二个(并返回两个数据帧的列表)。
dfs <- list(data.frame(x = 1), data.frame(y = 2))
mapply(dplyr::mutate, dfs, z = 3:4, SIMPLIFY = FALSE)
#> Error in lazyeval::lazy_dots(...): Promise has already been forced
What's going wrong for me, and how should I go about doing what I want to do? 对我来说出了什么问题,我该怎么做才能做我想做的事情?
Using an anonymous function works: 使用匿名函数有效:
mapply(function(df, z) mutate_(df, z=z), dfs, 3:4, SIMPLIFY=FALSE)
#[[1]]
# x z
#1 1 3
#
#[[2]]
# y z
#1 2 4
You could also do this with map2
from the purrr
package if you want to stay in the tidyverse
: 如果你想留在tidyverse
你也可以使用purrr
包中的map2
来做到这tidyverse
:
library(purrr)
library(dplyr)
map2(dfs, 3:4, ~ .x %>% mutate(z=.y))
[[1]] xz 1 1 3 [[2]] yz 1 2 4
You can also pipe the list into map2
: 您还可以将列表map2
到map2
:
dfs %>% map2(3:4, ~ .x %>% mutate(z=.y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.