Patrick Balada
Patrick Balada

Reputation: 1450

How can I replace multiple characters in R using dplyr?

I wrote a function looking as follows:

special_char <- function(data_in) {
  data_in=gsub("à","a",data_in)
  data_in=gsub("â","a",data_in)
  data_in=gsub("é","e",data_in)
  data_in=gsub("î","i",data_in)
  data_in=gsub("ä","ae",data_in)
  data_in=gsub("ö","oe",data_in)
  data_in=gsub("ü","ue",data_in)
  data_in=gsub("imp.","impessa",data_in)
  data_in=gsub("ch.","chemin",data_in)
  data_in=gsub("av.","avenue",data_in)
  data_in=gsub("str.","strasse",data_in)

  return(data_in)
}

Then, I try to apply it on my dataset using.

some_data %>% mutate_all(funs(special_char(.)))

However, the output is a mess. Does someone notice an obvious mistake in my approach?

Suppose I have the following input:

data_test <- data.frame(col1 = c("Céline", "Désiré", "Björn"))

I would expect to get the following output:

c("Celine", "Desire", "Bjoern")

Upvotes: 1

Views: 432

Answers (1)

Florian
Florian

Reputation: 25385

This works for me:

some_data  %>% mutate_all(funs(special_char))

I hope this also solves the issue for you. If not, what does your data look like?

Florian

Upvotes: 1

Related Questions