-2

Hi i Have data frame need to replace all special characters with "."

df
variabe
name:A-B c
name/A-B-c
name-A/B:c

i have tried 
df$variable =  gsub("-", ".", df$variable)
df$variable =  gsub(":", ".", df$variable)

.. Is there any function / modification to do in a single step

o/p
variabe
name.A.B.c
name.A.B.c
name.A.B.c

Thanks

Srm Murty
  • 105
  • 8

1 Answers1

1

We can use the [[:punct:]] to match any punctuation characters and replace with .

gsub("[[:punct:]]+", ".", df$variabe)
akrun
  • 674,427
  • 24
  • 381
  • 486
  • you don't need the `+` and depending on the characters to replace, maybe just `gsub("[:-)]", ".", df$variable)` – Cath Jul 11 '17 at 12:55
  • 1
    @Cath If there are patterns like `"A::B_` and want to replace with a single `.` – akrun Jul 11 '17 at 12:58
  • ok but it is not sure whether OP would like to replace that with a single dot or 2 though – Cath Jul 11 '17 at 13:02