R Tip: Use Named Vectors to Re-Map Values

March 28, 2018

(This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers)

Here is an R tip. Want to re-map a column of values? Use a named vector as the mapping.



head(starwars[, qc(name, gender)])

# # A tibble: 6 x 2
# name           gender
#   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
# 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():


# [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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)