简体   繁体   中英

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?

@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='')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM