子集行仅包含R中的字母

子集行仅包含R中的字母,r,subset,letters,R,Subset,Letters,我的向量大约有3000个观测值,如: clients <- c("Greg Smith", "John Coolman", "Mr. Brown", "John Nightsmith (father)", "2 Nicolas Cage") clients我们可以使用grep只匹配大写或小写字母以及字符串从开始(^)到结束($)的空格 grep('^[A-Za-z ]+$', clients, value = TRUE) #[1] "Greg Smith" "John Coolman

我的向量大约有3000个观测值,如:

clients <- c("Greg Smith", "John Coolman", "Mr. Brown", "John Nightsmith (father)", "2 Nicolas Cage")

clients我们可以使用
grep
只匹配大写或小写字母以及字符串从开始(
^
)到结束(
$
)的空格

grep('^[A-Za-z ]+$', clients, value = TRUE)
#[1] "Greg Smith"   "John Coolman"
或者只需使用
[:alpha:]+

grep('^[[:alpha:] ]+$', clients, value = TRUE)
#[1] "Greg Smith"   "John Coolman"