从包含 R 中非字母字符的列表中跳过单词

Skipping words from a list that contain non-alpha characters in R

我想知道从一长串包含非字母字符的单词中跳过所有单词的最快、最可靠的方法是什么?

输入应如下所示:

words = c('one', 'two', 'three,', 'four', '.five', 'others\'', 'ma-ny')

由此产生的新列表应该是:

newWords = c('one', 'two', 'four')

gsub()tidyversestringr 包中的东西?非常感谢!

我们可以使用grep,指定模式从开头(^)到结尾($)只有一个或多个字母([[:alpha:]]+) ) 的字符串

grep("^[[:alpha:]]+$", words, value = TRUE)
#[1] "one"  "two"  "four"

要么按照 akun 的建议使用 base R 中的 grep,要么你可以包 stringr:

library(stringr)
str_subset(words, "^[:alpha:]+$")