How can I apply a filter in R using only the non-missing arguments of a function?
Let’s say I have this function:
extract_data <- function(.a = NA, .b = NA, .c = NA) {
df %>%
filter(a == .a,
b == .b,
c == .c)
}
If I run extract_data(.a = 1, .c == 1), I’d like the function to return all rows of df where a = 1 and c = 1, regardless of the value of b.
Clearly, the function above takes into account the value of b and will only return rows where a = 1, c = 1, and b = NA. How can I modify the function to ignore b when its value is NA?
I am looking for a dynamic solution since my actual function has more arguments than used in the example above.