0

I have a single column of words that I am trying to clean. Some of the words have characters in them that I would like replaced with a space.

I know how to replace a single character in a string:

df2 <- data.frame(gsub("-"," ",data$string_column))

This example replaces the '-' character with a space.

How do I apply this procedure to an array of characters? I have tried the following:

df2 <- data.frame(gsub(c("-","&")," ",data$string_column))

This code runs, but it will only perform the operation of the first character, and not the second.

Any ideas on how to define a list of characters to be replaced by a space?

Thank you

Clifford Piehl
  • 483
  • 1
  • 4
  • 11
  • 3
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It seems like you want `df2 <- data.frame(gsub("[-&]", " ", data$string_column))`. The `gsub` allows for regular expressions so you just need the appropriate regular expression. – MrFlick Aug 25 '22 at 18:08
  • 1
    `data$string_column <- gsub("[-&]", " ", data$string_column)` – Wiktor Stribiżew Aug 25 '22 at 18:19

1 Answers1

0

You need

data$string_column <- gsub("[-&]", " ", data$string_column)

This way, all - and & chars in the string_column of the data dataframe will get replaced with a space char.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563