Filter by variable column name

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.

Hi, following webinar is a really useful resource on tidy eval.

You can achieve your result as follows:

library(tidyverse)
library(rlang)
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. <- rlang::quo(g1)

df %>%
  filter(!is.na(!!name.))
2 Likes

Thank you. In the example you gave, the column name is given as g1. I am looking for a solution where the column name is given as a string.

library(tidyverse)
library(rlang)
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"
name_sym <- rlang::sym(name.)

df %>%
  filter(!is.na(!!name_sym))
1 Like

Thank you very much for the help.