This function is useful for filtering dataframes by list columns. It checks if an element is contained in each list element, returning a logical vector. Note that this is different than %in% which does not work with list columns in a natural manner.

element %in_list% list

Arguments

element

The element to check for

list

A list of elements to check against

Value

A logical vector indicating if the element is in each list element

Examples

library(dplyr)

df <- tibble(id = c("A", "B", "C"), list_col = list(1:3, 2:4, 3:5))

# works naturally for list columns
df %>% filter(2 %in_list% list_col)
#> # A tibble: 2 × 2
#>   id    list_col 
#>   <chr> <list>   
#> 1 A     <int [3]>
#> 2 B     <int [3]>

# also works with basic vector columns
df %>% filter("B" %in_list% id)
#> # A tibble: 1 × 2
#>   id    list_col 
#>   <chr> <list>   
#> 1 B     <int [3]>

# does not work with multi-valued left hand sides
# df %>% filter(c("B", "C") %in_list% id) # error