在整理中使用动态名称
我想用变量名称为“对象”将变量围起来。我在突变中使用动态名称有问题。 在这里示例数据:
df <- structure(list(f116_gr = c(1, 2, 3, 5, 4, 6, NA),
f116_gr_sc = c(-1.36988327957453,-0.144575006139099, 0.63865967223421,
1.23657391288124, 1.53907997023586,-0.280185492819537, 0)),
row.names = c(NA, -7L), class = c("tbl_df","tbl", "data.frame"))
我可以使用此代码进行操作:
z <- "f116_gr_sc"
df[, eval(z)] = round(df[, eval(z)], 2)
但是当我使用突变功能尝试时,有一个错误:
library(dplyr)
z <- "f116_gr_sc"
df %>%
mutate(!!z := round(paste0(!!z), 2))
我该如何修复,非常感谢
I want to round the variable with name of variable is object.I have problem with using dynamic name in mutate.
Here sample data:
df <- structure(list(f116_gr = c(1, 2, 3, 5, 4, 6, NA),
f116_gr_sc = c(-1.36988327957453,-0.144575006139099, 0.63865967223421,
1.23657391288124, 1.53907997023586,-0.280185492819537, 0)),
row.names = c(NA, -7L), class = c("tbl_df","tbl", "data.frame"))
I can do it with this code:
z <- "f116_gr_sc"
df[, eval(z)] = round(df[, eval(z)], 2)
But when i try it using mutate function, there is a error:
library(dplyr)
z <- "f116_gr_sc"
df %>%
mutate(!!z := round(paste0(!!z), 2))
How can i fix it, thank all very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
as.name
将有所帮助。这将有效。
新
z
Add
as.name
will helps.This will works.
new
z
使用
sym
在2022-06-09创建的 v2.0.1)
use
sym
Created on 2022-06-09 by the reprex package (v2.0.1)