【发布时间】:2017-03-15 19:37:51
【问题描述】:
我的向量有大约 3000 个观察值,例如:
clients <- c("Greg Smith", "John Coolman", "Mr. Brown", "John Nightsmith (father)", "2 Nicolas Cage")
如何子集仅包含带有字母的名称的行。例如,只有 Greg Smith、John Coolman(没有 0-9、.?:[} 等符号)。
【问题讨论】:
我的向量有大约 3000 个观察值,例如:
clients <- c("Greg Smith", "John Coolman", "Mr. Brown", "John Nightsmith (father)", "2 Nicolas Cage")
如何子集仅包含带有字母的名称的行。例如,只有 Greg Smith、John Coolman(没有 0-9、.?:[} 等符号)。
【问题讨论】:
我们可以使用grep 仅匹配大写或小写字母以及从字符串的开头(^)到结尾($)的空格。
grep('^[A-Za-z ]+$', clients, value = TRUE)
#[1] "Greg Smith" "John Coolman"
或者只使用[[:alpha:] ]+
grep('^[[:alpha:] ]+$', clients, value = TRUE)
#[1] "Greg Smith" "John Coolman"
【讨论】: