Reorders the columns of a data frame so that columns whose names appear
in a DataDictionary come first, in the order they are defined in the
dictionary. Columns not present in the dictionary are optionally kept
at the end.
Arguments
- data
A data frame or tibble whose columns will be reordered.
- dictionary
A data dictionary object. If
NULL, uses the default dictionary set viaset_default_dictionary().- keep_unmatched
Logical. If
TRUE(the default), columns indatathat are not defined in the dictionary are retained and appended after the matched columns. IfFALSE, those columns are dropped from the output.
Value
The same type as data, with columns reordered (and
optionally filtered) according to the dictionary.
Examples
dd <- data_dictionary(
numeric_variable("bill_length_mm", label = "Bill length", units = "mm"),
numeric_variable("bill_depth_mm", label = "Bill depth", units = "mm"),
nominal_variable("species", label = "Species",
category_levels = c("Adelie", "Chinstrap", "Gentoo"))
)
df <- data.frame(
species = c("Adelie", "Chinstrap"),
extra_col = 1:2,
bill_depth_mm = c(18.7, 17.4),
bill_length_mm = c(39.1, 46.5)
)
# Matched columns appear in dictionary order; extra_col is appended
index_columns(df, dictionary = dd)
#> bill_length_mm bill_depth_mm species extra_col
#> 1 39.1 18.7 Adelie 1
#> 2 46.5 17.4 Chinstrap 2
# Drop columns not in the dictionary
index_columns(df, dictionary = dd, keep_unmatched = FALSE)
#> bill_length_mm bill_depth_mm species
#> 1 39.1 18.7 Adelie
#> 2 46.5 17.4 Chinstrap
