How can I use filter with a variable column name?
df <- tibble(
g1 = c(1, 1, 2, 2, 2, NA),
g2 = c(1, 2, 1, 2, 1, NA),
a = sample(6),
b = sample(6)
)
name. <- "g1"
df %>%
filter(!is.na(name.))
This is the result I am looking for:
df %>%
filter(!is.na(get(name.)))
# A tibble: 5 x 4
g1 g2 a b
<dbl> <dbl> <int> <int>
1 1.00 1.00 5 5
2 1.00 2.00 6 6
3 2.00 1.00 2 3
4 2.00 2.00 4 4
5 2.00 1.00 1 2
I would like to understand how to use the quo, enquo, and !! functions for this purpose.