How to 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 dplyr 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:

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

One Reply to “How to Replace String in Column Using dplyr”

  1. I wanted to let you know that the across syntax in dplyr has been deprecated. This is the warning:
    Caused by warning:
    ! The `…` argument of `across()` is deprecated as of dplyr 1.1.0.
    Supply arguments directly to `.fns` through an anonymous function
    instead.

    # Previously
    across(a:b, mean, na.rm = TRUE)

    # Now
    across(a:b, \(x) mean(x, na.rm = TRUE))

Leave a Reply

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