How to Replace String in Column in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Replace String in Column in R appeared first on Data Science Tutorials

How to Replace String in Column in R? using the dplyr package’s functions, you can replace a string in a particular column in a data frame in the following ways.

Data Science Statistics Jobs  » Are you looking for Data Science Jobs?

With the following data frame in R, the following examples demonstrate how to utilize each technique.

Let’s create a data frame

df <- data.frame(country=c('India', 'USA', 'CHINA', 'Algeria'),
                 position=c('1', '1', '2', '3'),
                 points=c(22, 25, 29, 13))

Now we can view the data frame

df
country position points
1   India        1     22
2     USA        1     25
3   CHINA        2     29
4 Algeria        3     13

Example 1: Add a new string in lieu of one.

Dealing With Missing values in R – Data Science Tutorials

The nation column’s string “India” can be changed to the string “Albania” using the code below.

library(dplyr)
library(stringr)

Now replace ‘India’ with ‘ Albania ‘ in-country column

df %>%
  mutate(across('country', str_replace, 'India', 'Albania'))
country position points
1 Albania        1     22
2     USA        1     25
3   CHINA        2     29
4 Algeria        3     13

In the country column, all instances of the string “India” have been changed to “Albania” while the other columns have remained the same.

Create new variables from existing variables in R – Data Science Tutorials

Example 2: Substituting a new string for several existing ones

The code below demonstrates how to substitute an empty string for the characters ‘I’ and ‘C’ in the country column:

library(dplyr)
library(stringr)

Let’s replace ‘I’ and ‘C’ with an empty string in the country column

df %>%
  mutate(across('country', str_replace, 'A|I', ''))
country position points
1    ndia        1     22
2      US        1     25
3    CHNA        2     29
4  lgeria        3     13

In the country, you’ll see that every ‘I’ and ‘C’ string has been changed to an empty string, but all other columns have remained the same.

Hypothesis Testing Examples-Quick Overview – Data Science Tutorials

Note that we instructed R to replace any strings matching either “I” or “C” with an empty string by using the “OR” (|) operator.

You are allowed to replace as many values in a column at once with as many “OR” (|) operators as you wish.

The post How to Replace String in Column in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)