Reputation: 323
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
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
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"
d <- c("sladfj0923rn2", "ääas230ß0sadfn", "823Höl32basdflk")
Upvotes: 1