简体   繁体   English

R:删除所有不匹配的字符串矢量字母

[英]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 "az", "Az" and "'" 我想从此向量中删除所有不匹配“ az”,“ Az”和“'”的字符

I tried to use gsub("![a-zA-z'], "", d) but that doesn't work. 我尝试使用gsub("![a-zA-z'], "", d)但这不起作用。

We could even make your replacement pattern even tighter by doing a case insensitive sub : 通过执行不区分大小写的sub我们甚至可以使您的替换模式更加严格:

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

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

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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