我编写了这个函数,它只是用其子字符串替换数据表列中的每个值:
substrColName <- function(df, colName, start) {
df %>% mutate(colName = substr(colName, start=start, stop=nchar(colName)))
}
但是每次我尝试运行它时都会收到错误:
Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'
现在我已经做了很多研究为什么它不起作用,但我无法弄清楚。我读过一些有关标准评估和惰性评估的内容,但我尝试的似乎都不起作用。有什么帮助吗?
谢谢
最佳答案
谢谢,@ycw,读得很好。看完这篇文章后,现在就可以使用了。这是最终的解决方案:
substrColName <- function(df, colName, start) {
colNameQuo <- enquo(colName)
df %>% mutate(!!quo_name(colNameQuo) := substr(!!colNameQuo, start=start,stop=nchar(!!colNameQuo)))
}
关于r - 如何将列名传递到使用 dplyr 的自定义函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46616162/