-2

How do I remove special characters like # from a dataset in R? I have dataset like this,

X Id           V1         V2
1 65          245         Good
2 41          245         Good
3 48          245            $
4 74          245         Good
6 54                      Good
7 60          245         Good

I tried to remove the special characters by using the following line

df2[, (1:3)] <- sapply(df2[, (1:3)], function(col) {
  as.numeric(sub("[*]$#", "", col))
})

but this is not working? Is their any way to remove the blank cells as well using the above code or in one same line?

MargS
  • 122
  • 2
  • 8
  • 1
    Hi user3448883, welcome to SO! If you want to get a quick answer, it would help to post a small example of the type of data you are working with, and exactly what you are looking for as far as output. Check out http://stackoverflow.com/a/5963610/554531 – Keith Hughitt Jan 03 '17 at 13:12

1 Answers1

2

With stringr try the following (on a string s):

library(stringr)
str_replace_all(s, "[[:punct:]]", "") # if you want to remove the punctuations
str_replace_all(s, "[^[:alnum:]]", "") # if you want to keep only the alphanumerics
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63