A new column is created by mapping each input label to some output label and storing. The mapping argument is of the form c(input_label = "output_label", ...). These can also be created programmatically with the use of setNames.

map_labels(df, mapping, inner_col, new_col)

Arguments

df

An data frame

mapping

A mapping for labels as a named vector.

inner_col

Column that contains labels

new_col

New column

Value

the same data frame with new column added

Examples

df <- data.frame(
 some_label_col = c("A", "B", "A")
 )

mapping <- c(
A = "Approve",
B = "Reject"
)

map_labels(df, mapping = mapping, inner_col = "some_label_col", new_col = "Decision")
#> # A tibble: 3 × 2
#>   some_label_col Decision
#>   <chr>          <chr>   
#> 1 A              Approve 
#> 2 B              Reject  
#> 3 A              Approve