How to Replace String in Column using R

How to Replace String in Column using 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 ‘A’ in the country column:

library(dplyr)
library(stringr)

Let’s replace ‘I’ and ‘A’ 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 ‘A’ 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 “A” 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.

You may also like...

Leave a Reply

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

12 − two =