Recode data in R, recode dplyr

Recode data in R, replace values

Here is how to recode data in R in 3 different ways. Some may call it an efficient way how to replace existing values with new values.

Recode data with dplyr

There is a dedicated function recode that you can use to recode necessary data in R. Here is how it works.

require(dplyr)

iris %>% count(Species, name = "Species.Count")

#     Species Species.Count
#1     setosa            50
#2 versicolor            50
#3  virginica            50

iris %>%
  count(Species, name = "Species.Count") %>%
  mutate(Species = recode(Species,
    "setosa" = "SET",
    "versicolor" = "VER",
    "virginica" = "VIR"
  ))

#  Species Species.Count
#1     SET            50
#2     VER            50
#3     VIR            50

There might be a desire to do the value recoding based on two vectors in dplyr recode, but I’m afraid that it is not possible to pass two vectors. The good news is that the next section is a solution to that.

 

Recode data based on two vectors with plyr

If recode in dplyr is not suitable for passing one vector with old values and the other with new values, then mapvalues from plyr works perfectly.

require(dplyr)

a <- c("setosa", "versicolor", "virginica")
b <- c("SET", "VER", "VIR") 

iris %>%
  count(Species, name = "Species.Count") %>%
  mutate(Species = plyr::mapvalues(Species, from = a, to = b))

#  Species Species.Count
#1     SET            50
#2     VER            50
#3     VIR            50

 

Recode data by using a data frame in the dplyr pipe

If you are passionate about using only the dplyr package, then here is a workaround. In addition, it is an example of how to use left_join in the dplyr pipe.

require(dplyr)

rdf <- data.frame(
  "Species" = c("setosa", "versicolor", "virginica")
  , "b" = c("SET", "SET", "SET")) 

iris %>%
  count(Species, name = "Species.Count") %>%
  left_join(., rdf, by = "Species") %>%
  select(-Species) %>%
  rename("Species" = b)

#  Species.Count Species
#1            50     SET
#2            50     SET
#3            50     SET

 

Other useful tools for similar work in R

If you are enjoying working with dplyr then here are my top 10 dplyr tips and tricks.

Sometimes you might not be interested in recoding existing values, but categorize numbers by user-defined groups. Here is how to do that.





Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *