Translate column names of a data.frame or lazy tibble using a dictionary
dr_translate.RdThis function allows renaming column names in an input object (a data.frame or tbl_lazy)
by translating them with a user-supplied dictionary. Users can choose to translate
column names from "old" to "new" or vice versa.
Arguments
- d
A
data.frameortbl_lazyobject whose column names need to be translated.- dictionary
A
data.frame(or tibble) with at least two columns:fromandto(default names are "old" and "new"). Thefromcolumn contains the current column names ind, and thetocolumn contains the new column names to translate to.- from
A string specifying the column name in
dictionaryto use for current column name matching. Defaults to "old".- to
A string specifying the column name in
dictionarywith new column names to translate to. Defaults to "new".
Examples
# Example dictionary
dictionary <- data.frame(
old = c("Ship", "SweepLngt"),
new = c("Platform", "SweepLength")
)
# Example data
df <- data.frame(Ship = "26D4", SweepLngt = 110)
dr_translate(df, dictionary)
#> Platform SweepLength
#> 1 26D4 110