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')"
jmount
Data Scientist and trainer at Win Vector LLC. One of the authors of Practical Data Science with R.
And the “
.
” is extra notation I recommend for clarity and teaching in pipes: R Tip: Make Arguments Explicit in magrittr/dplyr Pipelines.Just a note (and I wish I had re-checked this before posting), the direct mapping technique does not appear to work on remote (database)
dplyr
examples. However this is not a problem as the result is easy to achieve by a left_join().If I get this clear, I have to remap all values, is this correct? In my case I often want to remap just a few things and want to leave the other values unchanged… This will not be possible with your way of remapping. Am i correct?
That can be done with an
ifelse()
.