Suppose we have some vectors and dataframes:
a <- c(1, 2, 0, 1)
b <- c(6, 4)
df1 <- data_frame(x = c(6, 8, 12), y = c(24, 18, 16))
We write a function using non standard evaluation that calculates the mean of a column of the dataframe and the mean of a vector.
calculate_means <- function(df, column, vector) {
column <- enquo(column)
summarise(df, mean_column = mean(!!column), mean_vector = mean(vector))
}
calculate_means(df1, x, a)
# A tibble: 1 x 2
mean_column mean_vector
<dbl> <dbl>
1 8.67 1.00
calculate_means(df1, y, b)
# A tibble: 1 x 2
mean_column mean_vector
<dbl> <dbl>
1 19.3 5.00
That works as expected. But what happens if we write the same function but choosing another names for the parameters?
calculate_means <- function(df, x, y) {
x <- enquo(x)
summarise(df, mean_column = mean(!!x), mean_vector = mean(y))
}
calculate_means(df1, x, a)
# A tibble: 1 x 2
mean_column mean_vector
<dbl> <dbl>
1 8.67 19.3
calculate_means(df1, y, b)
# A tibble: 1 x 2
mean_column mean_vector
<dbl> <dbl>
1 19.3 19.3
The first parameter is evaluating the same as before, but the second parameter is always evaluating the column "y" of the dataframe. Shouldn't it be evaluating the vectors "a" and "b" respectively?