使用非标准评估错误评估函数的参数?

时间:2018-02-21 12:06:04

标签: r dplyr non-standard-evaluation

假设我们有一些向量和数据帧:

a <- c(1, 2, 0, 1)
b <- c(6, 4)
df1 <- data_frame(x = c(6, 8, 12), y = c(24, 18, 16))

我们使用非标准评估来编写函数,该评估计算数据框的列的平均值和向量的平均值。

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

按预期工作。但是如果我们编写相同的函数但为参数选择其他名称会发生​​什么呢?

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

第一个参数评估与之前相同,但第二个参数始终评估数据帧的列“y”。 不应该分别评估向量“a”和“b”吗?

2 个答案:

答案 0 :(得分:0)

我们可以使用globalenv()来获取对象列表,通过将对象名称作为字符串传递来获取对象的值,并在summarise语句中使用它

calculate_means <- function(df, x, y) {
  x <- enquo(x)
  y <- quo_name(enquo(y))
  v1 <- globalenv()[[y]]

  df %>%
       summarise(mean_column = mean(!! x),
       mean_vector = mean(v1))
 }

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

假设我们还需要获取'y'列的mean

calculate_means <- function(df, x, y) {
  x <- enquo(x)
  y <- quo_name(enquo(y))
  v1 <- globalenv()[[y]]

  df %>%
       summarise(mean_column = mean(!! x),
       mean_vector = mean(v1),
       mean_column2 = mean(.data$y))
 }

calculate_means(df1, x, a)
# A tibble: 1 x 3
#  mean_column mean_vector mean_column2
#        <dbl>       <dbl>        <dbl>
#1        8.67        1.00         19.3

答案 1 :(得分:0)

summarise的实际参数中的变量首先在summarise的第一个参数中指定的数据框中查找,并且仅在调用者中查找summarise(如果未找到)在该数据框架中。因此,通过将y硬编码到summarise参数中,它将始终在df1中查找它。

1)我们可以使用!!避免这种情况。争论!!未在数据参数中查找。

calculate_means2 <- function(df, x, y) {
  x <- enquo(x)
  summarise(df, mean_column = mean(!!x), mean_vector = mean(!!y))
}

calculate_means2(df1, y, b)
# A tibble: 1 x 2
  mean_column mean_vector
        <dbl>       <dbl>
1        19.3        5.00

2)我们可以使用as_quosure来强调这一点。这将把y的值放在quosure公式中。在示例中,y <- as_quosure(y)会导致新的quosure包含~c(6, 4)

calculate_means3 <- function(df, x, y) {
  x <- enquo(x)
  y <- rlang::as_quosure(y)
  summarise(df, mean_column = mean(!!x), mean_vector = mean(!!y))
}

calculate_means3(df1, y, b)
# A tibble: 1 x 2
  mean_column mean_vector
        <dbl>       <dbl>
1        19.3        5.00

3)当然,我们可以使用不太可能在数据框中使用的正式参数名称:

calculate_means4 <- function(df, x, y.) {
  x <- enquo(x)
  summarise(df, mean_column = mean(!!x), mean_vector = mean(y.))
}

calculate_means4(df1, y, b)
# A tibble: 1 x 2
  mean_column mean_vector
        <dbl>       <dbl>
1        19.3        5.00