R Tip: Use Named Vectors to Re-Map Values

[This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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')"

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)