`first_match_col()` finds the first matching column in a dataframe

first_match_col(x, pattern, how = c("all", "any"))

Arguments

x

A dataframe

pattern

A pattern to match

how

How to match the pattern (all or any)

Value

The name of the first matching column

Examples

data <- data.frame(a = c("a", "b", "c"), b = c("a", " ", "c"), c = c("a", "b", "1"))
first_match_col(data, "\\D", "all")
#> [1] "a"
first_match_col(data, "\\d", "any")
#> [1] "c"