【发布时间】:2019-10-24 20:48:11
【问题描述】:
我正在使用我编写的函数更改一列以在 R 中创建一个新列。
在我的函数中,我想发送一条消息,其中包含正在更改的列的名称。
如何在 mutate 调用中从函数内部访问正在变异的列名?
可重现的例子:
data <- tribble(
~colB,
1,
2,
3
)
# Function that will be used in the mutate
add1 <- function(numeric_vector) {
return(1 +numeric_vector)
# I want to message the user the name of the column they are mutating
# This simply returns the entire vector
message("You mutated", numeric vector)
# This returns 'numeric_vector'
message("You mutated", quo_name(quo(numeric_vector)))
}
# Desired Output:
data %>%
mutate(colC = add1(colB))
You mutated colB
colB colC
<dbl> <dbl>
1 2
2 3
3 4
【问题讨论】: