【发布时间】:2019-11-01 12:09:48
【问题描述】:
如果data.frame 中有一个列与我的函数的参数之一同名,则在尝试mutate 或filter 时会使用该列名。
有没有办法显式使用函数参数?
可重现的例子:
my_function <- function(x) {
my_tib <- tribble(
~x, ~y,
1, 5,
2, 10,
3, 15
)
my_tib %>%
mutate(z = x * y)
}
my_function(x=100)
## A tibble: 3 x 3
# x y z
# <dbl> <dbl> <dbl>
#1 1 5 5
#2 2 10 20
#3 3 15 45
想要的结果:
my_function(x=100)
## A tibble: 3 x 3
# x y z
# <dbl> <dbl> <dbl>
#1 1 5 500
#2 2 10 1000
#3 3 15 1500
【问题讨论】: