【发布时间】:2021-12-10 20:37:09
【问题描述】:
我想过滤数据框(包含单词)的行以仅保留由某些字母组成的单词。例如,假设我有一个数据框,例如:
library(tidyverse)
df <- data.frame(words = c("acerbe", "malus", "as", "clade", "after", "sel", "moineau") )
words
1 acerbe
2 malus
3 as
4 clade
5 after
6 sel
7 moineau
我只想保留由以下字母组成的行(单词)(并且只保留它们):
letters <- c("a", "z", "e", "r", "q", "s", "d", "f", "w", "x", "c")
换句话说,我想排除包含除上面列出的字母之外的其他字母的单词。
我尝试过使用 string::str_detect(),但到目前为止没有成功...
letters <- "a|z|e|r|q|s|d|f|w|x|c"
df <- data.frame(words = c("acerbe", "malus", "as", "clade", "after", "sel", "moineau") )
df %>% filter(str_detect(string = words, pattern = letters, negate = FALSE) )
words
1 acerbe
2 malus
3 as
4 clade
5 after
6 sel
7 moineau
【问题讨论】: