如何使用 tidyverse 中的相邻列修改在另一列中命名的列?
我有以下数据框,
df <- tibble(x = c(2, 3, 4)) %>%
mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55)
列 x
包含需要操作的列名称,该列中的值必须替换为列 x-1
x-1 中的值之和代码>、<代码>x和<代码>x+1。例如,对于 x
为 2 的第一行,2
列中的值必须替换为 (99+88+77) = 264。
我尝试使用双卷曲括号({{}}
) 和 :=
,如下所示,
df %>%
mutate("{{x}}" := {{x-1}} + {{x}} + {{x+1}})
但出现以下错误, local_error_context(dots =dots, .index = i, mask = mask) 中的错误:promise 已在评估中:递归默认参数引用或早期问题?
然后我尝试使用 cur_column( )
在 across()
内,如下所示,
df %>%
mutate(across(-x, ~if_else(x == cur_column(), {{cur_column()}}, .x)))
我得到了与上面相同的错误,我想我可能错误地使用了卷曲运算符,有人可以帮忙吗?
I've the following dataframe,
df <- tibble(x = c(2, 3, 4)) %>%
mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55)
column x
holds the column names which needs to be manipulated, the value in that column has to be replaced with the sum of values in columns x-1
, x
and x+1
. For example, for the first row where x
is 2, value in column 2
has to be replaced with (99+88+77) = 264.
I tried using double curly brackets({{}}
) and :=
, like below,
df %>%
mutate("{{x}}" := {{x-1}} + {{x}} + {{x+1}})
but I'm getting the following error,Error in local_error_context(dots = dots, .index = i, mask = mask) : promise already under evaluation: recursive default argument reference or earlier problems?
Then I tried accessing the column using cur_column()
inside across()
like below,
df %>%
mutate(across(-x, ~if_else(x == cur_column(), {{cur_column()}}, .x)))
and I'm getting the same error as above, I think I may be using the curly operator incorrectly, can someone help, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
{{}}
语法适用于将未计算的表达式传递给 dplyr 命令时,它不适用于捕获列值。大多数时候,在每行的不同列上执行不同的选项并不容易。一种替代方法是重塑数据,以便可以使用超前/滞后函数。然后你就可以向后旋转。
另一种选择是创建一个辅助函数,可以使用
rowwise()
访问cur_column()
和cur_data()
,以便创建不同的每行的转换。The
{{}}
syntax is for when you are passing unevaluated expressions to a dplyr command, it does not work for capturing column values.Most of the time it's not easy to do different options on different columns for each row. One alternative is to reshape your data so you can use lead/lag functions. Then you can pivot back.
Another alternative is to create a helper function that can access the
cur_column()
andcur_data()
withrowwise()
in order to create a different transformation for each row.也许这也符合你的目的:
Maybe this one also serves your purpose: