theamateurdataanalyst
theamateurdataanalyst

Reputation: 2834

Removing odd characters in R with gsub

I am currently in the process of doing some text analysis. I want to keep only alphanumeric characters but for some reason I am having trouble removing some pesky characters that I don't consider alphanumeric. Here's an example of what I am dealing with:

letters <- "ՄĄՄdasdas"
letters <- gsub("[^[:alnum:]]", "",letters)   
letters

> "ՄĄՄdasdas"

What am I doing wrong here?

Upvotes: 4

Views: 417

Answers (2)

Matthew Plourde
Matthew Plourde

Reputation: 44614

@konvas shows you how to use gsub correctly in this situation. The problem with your attempt is that those non-ASCII characters are considered alphabetic characters in your locale. Another option is to use iconv:

iconv(letters, to='ASCII', sub='')

Upvotes: 6

konvas
konvas

Reputation: 14346

Try gsub("[^A-Za-z0-9]", "", letters)

Upvotes: 3

Related Questions