用Repard2和Rlang突变柱
我正在尝试以下操作:
library(tidyverse)
library(rlang)
df <- data.frame(a = 1:2)
reduce2(list(df, df, df), letters[2:3], ~ mutate(.x, !!(.y) := 2:3))
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?
我确实知道将列突变为数据框的许多方法,但是我正在尝试学习rlang
。
预期输出:
a b c
1 1 2 2
2 2 3 3
I am trying the following:
library(tidyverse)
library(rlang)
df <- data.frame(a = 1:2)
reduce2(list(df, df, df), letters[2:3], ~ mutate(.x, !!(.y) := 2:3))
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?
I do know many ways of mutating columns to a dataframe, but I am trying to learn rlang
.
The expected output:
a b c
1 1 2 2
2 2 3 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种将
purrr :: redive()
和rlang
IS组合的方法: 。
A method to combine
purrr::reduce()
andrlang
is:where the trick is to assign
df
to the argument.init
.我认为
redain 2
在这里不是正确的功能,因为您实际上并未使用任何项目,即第一次迭代后的数据帧列表。传递给redy2
的函数采用三个参数 - 第一个是要减少的对象,第二个是.x .x
中的下一个项目,第三个是下一个项目.y
。这意味着您仍然可以使用
REDAL2
,请注意:但请注意,您没有在功能主体中使用第二个参数。您只需使用
降低
:I don't think
reduce2
is the correct function here, since you aren't actually using any items an the list of data frames after the first iteration. The function that is passed toreduce2
takes three arguments - the first is the object being reduced, the second is the next item in.x
and the third being the next item in.y
.That means you can still use
reduce2
if you want, by doing:But note that you are not using the second argument in the function body. You could do it just with
reduce
:我确定您知道您可以做
df [letters [2:3]]&lt; -2:3
实现相同的输出。要使用
purrr
和rlang
您可以使用 -另一种方法是 -
I am sure you are aware that you can do
df[letters[2:3]] <- 2:3
to achieve the same output but I don't think this is what you are looking for.To use
purrr
andrlang
you may use -And another way would be -