6 C
London
Tuesday, March 11, 2025
HomeTidyverse in Rdplyr in RHow to Replace String in Column Using dplyr

How to Replace String in Column Using dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories