【发布时间】:2020-05-28 22:51:18
【问题描述】:
我想分配一个动态名称,即我传递给函数的变量的名称,作为函数正在创建的数据框中的列名。
我试过了
- deparse(substitute(x))
- toString(x)
但没有成功...
代码
a <- (1:3)
b <- (5:7)
df <- data.frame(a,b)
fun <- function(x){
x %>% mutate(c=a+b)
colnames(x)[3] <- deparse(substitute(x))
}
预期行为
运行 fun(df) 后:
a b df
1 1 5 6
2 2 6 8
3 3 7 10
改为:
> fun(df)
Error in names(x) <- value :
'names' attribute [3] must be the same length as the vector [2]
【问题讨论】:
标签: r