Iskandar The Pupsi
Iskandar The Pupsi

Reputation: 323

R: Drop all not matching letters of string vector

I have a string vector

d <- c("sladfj0923rn2", ääas230ß0sadfn", 823Höl32basdflk")

I want to remove all characters from this vector that do not match "a-z", "A-z" and "'"

I tried to use gsub("![a-zA-z'], "", d) but that doesn't work.

Upvotes: 2

Views: 34

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

We could even make your replacement pattern even tighter by doing a case insensitive sub:

d <- c("sladfj0923rn2", "ääas230ß0sadfn", "823Höl32basdflk")
gsub("[^a-z]", "", d, ignore.case=TRUE)

[1] "sladfjrn"  "assadfn"   "Hlbasdflk"

Upvotes: 2

akrun
akrun

Reputation: 887048

We can use the ^ inside the square brackets to match all characters except the one specified within the bracket

gsub("[^a-zA-Z]", "", d)
#[1] "sladfjrn"  "assadfn"   "Hlbasdflk"

data

d <- c("sladfj0923rn2", "ääas230ß0sadfn", "823Höl32basdflk")

Upvotes: 1

Related Questions