When updating to newest version of the package dplyr, a funcion I have using NSE breaks. I was wondering how the new version changes this and how to fix it. I've tried using the .data$
and .env$
before each variable name but can't seem to get it working.
Here is my custom function:
t_ppond <- function(w, v){
arguments <- as.list(match.call())
y <- eval(arguments$w)
x <- eval(arguments$v)
d <- data.frame("yy" = y,
"xx" = x)
tt <- sum(d$yy)
dff <- d %>%
mutate("sh" = yy/tt) %>%
mutate("rr" = xx*sh)
sum(dff$rr)
}
And this is what I used it for (calculating a weighted mean from a variable):
data(iris)
iris %>%
group_by(Species) %>%
summarise("new" = t_ppond(Sepal.Length, Petal.Width))
The above code worked perfectly before the update. Now I get:
Error in summarise_impl(.data, dots) :
Evaluation error: object 'Sepal.Length' not found.
Are you sure you need non-standard evaluation for this? You have a custom function that just needs to take in two vectors, so you can write it like:
t_ppond <- function(w, v){
d <- data.frame("yy" = w,
"xx" = v)
tt <- sum(d$yy)
dff <- d %>%
mutate("sh" = yy/tt) %>%
mutate("rr" = xx*sh)
sum(dff$rr)
}
data(iris)
iris %>%
group_by(Species) %>%
summarise("new" = t_ppond(Sepal.Length, Petal.Width))
Output:
# A tibble: 3 x 2
Species new
<fctr> <dbl>
1 setosa 0.2480224
2 versicolor 1.3352089
3 virginica 2.0333030