This function takes a character string and recodes it. Default recoding functions are available in functions such as tc_recode_vehicle_type() and tc_recode_casualties.

tc_recode(x, pattern = NULL, pattern_match = NULL)

Arguments

x

Character string to recode

pattern

A named character vector with values representing new values. Has the form c("Car long name" = "Car", "Taxi/Private hire car" = "Taxi"). Car long name will be converted into Car in this case.

pattern_match

Character string in the form of c("car" = "car", "bike" = "bike") that replaces all parts of the match.

Details

It is based on the function stringr::str_replace_all().

Examples

x = c("car long name", "bus", "a bike long", "a bike")
tc_recode(x, pattern = c("car.+" = "car"))
#> [1] "car"         "bus"         "a bike long" "a bike"     
tc_recode(x, pattern = c(".+bike.+" = "bike"))
#> [1] "car long name" "bus"           "bike"          "a bike"       
tc_recode(x, pattern = c("car.+" = "car", ".+bike.+" = "bike"))
#> [1] "car"    "bus"    "bike"   "a bike"
tc_recode(x, pattern_match = c("car" = "car", "bike" = "bike"))
#> [1] "car"  "bus"  "bike" "bike"