Table of Contents
Using the dplyr package, it is possible to replace multiple values in a data frame by using the replace_all() function. This function takes two arguments, the name of the data frame and a list of the values to be replaced. The syntax is dataframe %>% replace_all(list(old_value1 = new_value1, old_value2 = new_value2)). This function will replace all occurrences of the old values with the new values in the specified data frame.
You can use the following basic syntax to replace multiple values in a data frame in R using functions from the package:
library(dplyr) 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
df
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:
library(dplyr) #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.
The following tutorials explain how to perform other common tasks using dplyr: