How to programmatically filter columns in dplyr?

2020-07-26 11:17发布

How would I create a function that drops NA values in a column if I don't want to specify the column until the function is called?

minimal_case <- function(column_name = "a") {
  enquo_name <- enquo(column_name)

  example <- tibble(a = c(NA, 1))

  print(filter(example, !is.na(a)))

  print(filter(example, !is.na(rlang::UQ(enquo_name))))

}

The output of the first print statement is:

# A tibble: 1 x 1
      a
  <dbl>
1     1

The output of the second print statement is:

# A tibble: 2 x 1
      a
  <dbl>
1    NA
2     1

How do I get the second print statement to match the first?

2条回答
相关推荐>>
2楼-- · 2020-07-26 12:15

It seems the column_name parameter is a string. In that case, you can use rlang::sym:

minimal_case <- function(column_name = "a") {
    example <- tibble(a = c(NA, 1))

    print(filter(example, !is.na(a)))

    print(filter(example, !is.na(!!rlang::sym(column_name))))

}
查看更多
女痞
3楼-- · 2020-07-26 12:15

There is a good write-up on how to do things like this in dplyr here: http://dplyr.tidyverse.org/articles/programming.html

The punchline for this case is that you don't have to quote a in the parameters. You can also use !! instead of UQ

minimal_case <- function(column_name = a) {
    enquo_name <- enquo(column_name)

    example <- tibble(a = c(NA, 1))

    print(filter(example, !is.na(a)))

    print(filter(example, !is.na(!!enquo_name)))

}
查看更多
登录 后发表回答