How can I use dplyr to replace multiple values in a data frame?

How can I use dplyr to replace multiple values in a data frame?

Dplyr is a popular R package used for data manipulation and analysis. One useful function of dplyr is the ability to replace multiple values in a data frame. This can be done by using the “mutate” function and specifying the column and values to be replaced using the “case_when” function. By using dplyr, users can efficiently and accurately replace multiple values in their data frames, making data cleaning and manipulation tasks more streamlined and manageable.

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 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.

Additional Resources

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

Cite this article

stats writer (2024). How can I use dplyr to replace multiple values in a data frame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-multiple-values-in-a-data-frame/

stats writer. "How can I use dplyr to replace multiple values in a data frame?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-multiple-values-in-a-data-frame/.

stats writer. "How can I use dplyr to replace multiple values in a data frame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-multiple-values-in-a-data-frame/.

stats writer (2024) 'How can I use dplyr to replace multiple values in a data frame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-multiple-values-in-a-data-frame/.

[1] stats writer, "How can I use dplyr to replace multiple values in a data frame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use dplyr to replace multiple values in a data frame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top