Given a data frame with columns specifying hierarchically-nested levels, find the maximum non-missing hierarchical level for each row.
Usage
max_levels(x, pattern, by, type = c("index", "name"))Arguments
- x
a data frame containing hierarchical columns
- pattern
regex pattern to match the names of the hierarchical columns in
ref(supply eitherpatternorby)- by
vector giving the names of the hierarchical columns in
ref(supply eitherpatternorby)- type
type of return, either "index" to return integer indices (starting at 1) or "name" to return column names (as matched by
patternorby)
Value
Vector of indices or names corresponding to the maximum non-missing hierarchical level for each row
Examples
data(ne_ref)
# return integer indices (starting at 1)
max_levels(ne_raw, pattern = "^adm")
#> [1] 3 2 3 3 3 3 3 3 3 3 2 2 3 3 3
# return column names
max_levels(ne_raw, pattern = "^adm", type = "name")
#> [1] "adm2" "adm1" "adm2" "adm2" "adm2" "adm2" "adm2" "adm2" "adm2" "adm2"
#> [11] "adm1" "adm1" "adm2" "adm2" "adm2"