问题
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