How to programmatically filter columns in dplyr?

天涯浪子 提交于 2020-05-12 04:54:31

问题


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?


回答1:


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))))

}



回答2:


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)))

}


来源:https://stackoverflow.com/questions/45625769/how-to-programmatically-filter-columns-in-dplyr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!