Functionality like tidyverse's any_of()?

Is there any way to achieve functionality like tidyverse’s any_of()?

My use case is that I am reading only certain columns of a file, but there could be columns specified in the call, but non-existent in the file:

vars = ["one", "two", "three"] # column two does not exist in the file
df = read_csv(file, col_select=any_of(vars))

For this specific use case, CSV.read (Reading · CSV.jl) seems to accept keyword arguments select or drop that take a vector of column names (strings or symbols), positions (integers), or booleans (examples) in a way that invalid column names are just ignored.

2 Likes
using CSV

data = """
a,b,c
1,2,3
4,5,6
7,8,9
"""

vars = Symbol.(["a","g","c"])

file = CSV.File(IOBuffer(data); select=(i, name) -> name in vars)

# output:

3-element CSV.File:
 CSV.Row: (a = 1, c = 3)
 CSV.Row: (a = 4, c = 6)
 CSV.Row: (a = 7, c = 9)
1 Like