How can I use dplyr to replace a specific string in a column?

How can I use dplyr to replace a specific string in a column?

Dplyr is a popular data manipulation package in R that allows users to easily perform data wrangling tasks. One useful feature of dplyr is the ability to replace a specific string in a column of a data frame. This can be achieved by using the dplyr function “mutate” and the “str_replace” function from the stringr package. By combining these functions, users can efficiently replace a specific string in a column with a desired value, making data cleaning and transformation processes more streamlined and efficient.

Replace String in Column Using dplyr


You can use the following methods to replace a string in a specific column of a data frame using functions from the package:

Method 1: Replace One String with New String

library(dplyr)
library(stringr) 

df %>% 
  mutate(across('column_name', str_replace, 'old_value', 'new_value'))

Method 2: Replace Multiple Strings with New String

library(dplyr)
library(stringr) 

df %>% 
  mutate(across('column_name', str_replace, 'old_value1|old_value2', 'new_value'))

The following examples show how to use each method with the following data frame in R:

#create data frame
df <- data.frame(conf=c('East', 'East', 'West', 'West'),
                 position=c('P_Guard', 'P_Guard', 'S_Guard', 'S_Guard'),
                 points=c(22, 25, 29, 13))

#view data frame
df

  conf position points
1 East  P_Guard     22
2 East  P_Guard     25
3 West  S_Guard     29
4 West  S_Guard     13

Example 1: Replace One String with New String

The following code shows how to replace the string ‘East’ in the conf column with the string ‘Eastern’:

library(dplyr)
library(stringr)

#replace 'East' with 'Eastern' in conf column
df %>% 
  mutate(across('conf', str_replace, 'East', 'Eastern'))

     conf position points
1 Eastern  P_Guard     22
2 Eastern  P_Guard     25
3    West  S_Guard     29
4    West  S_Guard     13

Notice that each ‘East’ string has been replaced with ‘Eastern’ in the conf column, while all other columns have remain unchanged.

Example 2: Replace Multiple Strings with New String

The following code shows how to replace the string ‘P_’ and ‘S_’ in the conf column with an empty string:

library(dplyr)
library(stringr)

#replace 'P_' and 'S_' with empty string in position column
df %>% 
  mutate(across('position', str_replace, 'P_|S_', ''))

  conf position points
1 East    Guard     22
2 East    Guard     25
3 West    Guard     29
4 West    Guard     13

Notice that each ‘P_’ and ‘S_’ string have been replaced with an empty string in the position column, while all other columns have remain unchanged.

Note that we used the “OR” ( | ) operator to tell R that we’d like to replace any strings equal to ‘P_’ or ‘S_’ with an empty string.

Feel free to use as many “OR” ( | ) operators as you’d like to replace as many values as you’d like in a column at once.

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 a specific string in a column?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-a-specific-string-in-a-column/

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

stats writer. "How can I use dplyr to replace a specific string in a column?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-replace-a-specific-string-in-a-column/.

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

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

stats writer. How can I use dplyr to replace a specific string in a column?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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