riders994
riders994

Reputation: 1306

String Cleaning in R down to just letters

so I'm trying to clean strings in R, and I want to be able to clean every string of all of the non-letter elements. I know I can just use gsub to do them individually, but I want to be able to take out all of them (excluding spaces), or if I get it down to just individual character strings with spaces removed, stripping those as well.

For example, if i have an email address as a string vector, "[email protected]", or a string vector of the form ("abc", "123", "abc123"), stripping them should yield "abcgmailcom" and ("abc", "", "abc") respectively. I'm only going to be working with vectors of multiple strings.

Upvotes: 1

Views: 1354

Answers (1)

Roland
Roland

Reputation: 132959

Like this?

char <- c("dc2 54üx*","%67{~\\hjkh")
#[1] "dc2 54üx*"   "%67{~\\hjkh"
gsub("[^[:alpha:] ]","",char)
#[1] "dc üx" "hjkh"

Upvotes: 3

Related Questions