VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-use-str_replace-in-r/

⇱ How to Use str_replace in R? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Use str_replace in R?

Last Updated : 2 Dec, 2021

str_replace() is used to replace the given string with a particular value in R Programming Language. It is available in stringr library, so we have to load this library.

Syntax:

str_replace( "replacing string", "replaced string")

where,

  • replacing string is the string to be replaced
  • replaced string is the final string

We will use str_replace in dataframe. We can replace particular string in dataframe column by using the following syntax

str_replace(dataframe$column_name, "replacing string", "replaced string")

where,

  • dataframe is the input dataframe
  • column_name is the column in the dataframe

Example:

Output:

[1] "oops" "python" "php" 
[1] "HTML5" "css" "jsp" 

Method 2: Replace String with Nothing

We can replace the string with "" empty.

Syntax:

str_replace(dataframe$column_name, "replacing string", "")

Example :

Output:

[1] "" "python" "php" 
[1] "" "css" "jsp"

Method 3: Replace Multiple Strings

We can replace multiple string in a particular column by using str_replace_all method.

Syntax:

str_replace_all(dataframe$column_name, c("string1" = "new string",.................,"stringn" = "new string"))

Example:

Output:

[1] "oops" "python" "sql" 
[1] "R" "css" "servlets"
Comment
Article Tags:

Explore