我有以下功能,我希望通过标准评估传递给mutate
。但是,我无法想象如何从中构建公式。
library(dplyr)
library(lazyeval)
# Define custom function
.dist <- function(x, y) {
d <- sqrt(sum((x - y)^2))
return(d)
}
# NSE example
iris %>%
rowwise() %>%
mutate(newVar = .dist(c(Petal.Width, Petal.Length), c(Sepal.Width, Sepal.Length))) %>%
tbl_df()
# SE attempt
iris %>%
rowwise() %>%
mutate_(newVar = interp(~.dist(x, y),
x = c(as.name('Petal.Width'), as.name('Petal.Length')),
y = c(as.name('Sepal.Width'), as.name('Sepal.Length')))) %>%
tbl_df()