R Tip: Use Named Vectors to Re-Map Values
Win-Vector Blog 2018-03-28
Here is an R tip. Want to re-map a column of values? Use a named vector as the mapping.
Example:
library("dplyr") library("wrapr") head(starwars[, qc(name, gender)]) # # A tibble: 6 x 2 # name gender # <chr> <chr> # 1 Luke Skywalker male # 2 C-3PO NA # 3 R2-D2 NA # 4 Darth Vader male # 5 Leia Organa female # 6 Owen Lars male
(For qc()
please see R Tip: Use qc() For Fast Legible Quoting.)
Now suppose we want to remap the gender designations to a more concise notation.
The key point is to specify the transformation as data, and not as code. This can be efficient and easy to maintain. For our example we will use qc()
(from wrapr
) to create the named vector we wish to use a the map (and optionally also :=
).
map <- qc(female = F, hermaphrodite = H, male = M, none = N) # # or if we want to use := to write this # # in names := values assignment style # map <- qc(female, hermaphrodite, male, none) := # qc(F, H, M, N )
It is then a simple matter to transform the column (using either base R or dplyr style notations).
# base R version of the mapping starwars$gender <- map[starwars$gender] # # dplyr version of the mapping # starwars <- starwars %>% mutate(., gender = map[gender]) head(starwars[, qc(name, gender)]) # # A tibble: 6 x 2 # name gender # <chr> <chr> # 1 Luke Skywalker M # 2 C-3PO NA # 3 R2-D2 NA # 4 Darth Vader M # 5 Leia Organa F # 6 Owen Lars M
This sort of “using a vector as a mapping function” is often easier than a join or nested if or case statement.
For a code-like presentation of named vectors, try map_to_char()
:
map_to_char(map) # [1] "c('female' = 'F', 'hermaphrodite' = 'H', 'male' = 'M', 'none' = 'N')"