Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple strings in one gsub() or chartr() statement in R?

Tags:

string

r

gsub

I have a string variable containing alphabet[a-z], space[ ], and apostrophe['],eg. x <- "a'b c" I want to replace apostrophe['] with blank[], and replace space[ ] with underscore[_].

x <- gsub("'", "", x) x <- gsub(" ", "_", x) 

It works absolutely, but when I have a lot of condition, the code becomes ugly. Therefore, I want to use chartr(), but chartr() can't deal with blank, eg.

x <- chartr("' ", "_", x)  #Error in chartr("' ", "_", "a'b c") : 'old' is longer than 'new' 

Is there any way to solve this problem? thanks!

like image 784
Eric Chang Avatar asked Nov 27 '15 03:11

Eric Chang


People also ask

How do I replace multiple strings in R?

Use str_replace_all() method of stringr package to replace multiple string values with another list of strings on a single column in R and update part of a string with another string.

What is gsub () in R?

gsub() function in R Language is used to replace all the matches of a pattern from a string. If the pattern is not found the string will be returned as it is. Syntax: gsub(pattern, replacement, string, ignore.case=TRUE/FALSE)

How do you replace more than one string?

Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.

What is the difference between sub and gsub in R?

The sub() and gsub() function in R is used for substitution as well as replacement operations. The sub() function will replace the first occurrence leaving the other as it is. On the other hand, the gsub() function will replace all the strings or values with the input strings.


2 Answers

You can use gsubfn

library(gsubfn) gsubfn(".", list("'" = "", " " = "_"), x) # [1] "ab_c" 

Similarly, we can also use mgsub which allows multiple replacement with multiple pattern to search

mgsub::mgsub(x, c("'", " "), c("", "_")) #[1] "ab_c" 
like image 160
Ronak Shah Avatar answered Oct 11 '22 00:10

Ronak Shah


I am a fan of the syntax that the %<>% and %>% opperators from the magrittr package provide.

library(magrittr)  x <- "a'b c"  x %<>%   gsub("'", "", .) %>%   gsub(" ", "_", .)  x ##[1] "ab_c" 

gusbfn is wonderful, but I like the chaining %>% allows.

like image 34
Peter Avatar answered Oct 11 '22 01:10

Peter