Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove regular expressions in a string in R?

Tags:

string

regex

r

The string is "\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\tLocation\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tSan Francisco, CA;Oakland, CA" and I want it to be shown as "Location","San Francisco, CA;Oakland, CA".

Is there a function for this? Thanks!

like image 969
user1787675 Avatar asked Feb 19 '23 04:02

user1787675


1 Answers

You can use a regular expression to remove the special characters:

x <-"\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\tLocation\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tSan Francisco, CA;Oakland, CA"

gsub('[\r\n\t]', '', x)

However, if you really have a string with all those \ the answer becomes something more like:

gsub('\\\\[a-z]', '', x)
like image 111
Justin Avatar answered Feb 20 '23 22:02

Justin