How to Replace Multiple Values in Data Frame Using dplyr

You can use the following basic syntax to replace multiple values in a data frame in R using functions from the dplyr package:


df %>%
  mutate(var1 = recode(var1, 'oldvalue1' = 'newvalue1', 'oldvalue2' = 'newvalue2'), 
         var2 = recode(var2, 'oldvalue1' = 'newvalue1', 'oldvalue2' = 'newvalue2'))

The following example shows how to use this syntax in practice.

Example: Replace Multiple Values Using dplyr

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(conf=c('East', 'East', 'West', 'West', 'North'),
                 position=c('Guard', 'Guard', 'Guard', 'Guard', 'Forward'),
                 points=c(22, 25, 29, 13, 18))

#view data frame

   conf position points
1  East    Guard     22
2  East    Guard     25
3  West    Guard     29
4  West    Guard     13
5 North  Forward     18

Now suppose we would like to replace the following values in the data frame:

  • ‘conf’ column:
    • Replace ‘East’ with ‘E’
    • Replace ‘West’ with ‘W’
    • Replace ‘North’ with ‘N’
  • ‘position’ column:
    • Replace ‘Guard’ with ‘G’
    • Replace ‘Forward’ with ‘F’

We can use the mutate() and recode() functions to do so:


#replace multiple values in conf and position columns
df %>%
  mutate(conf = recode(conf, 'East' = 'E', 'West' = 'W', 'North' = 'N'), 
         position = recode(position, 'Guard' = 'G', 'Forward' = 'F'))

  conf position points
1    E        G     22
2    E        G     25
3    W        G     29
4    W        G     13
5    N        F     18

Notice that each of the values in the ‘conf’ and ‘position’ columns have been replaced with specific values.

Also notice that the values in the ‘points’ column have remain unchanged.

Additional Resources

The following tutorials explain how to perform other common tasks using dplyr:

How to Recode Values Using dplyr
How to Replace NA with Zero in dplyr
How to Filter Rows that Contain a Certain String Using dplyr

Leave a Reply

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