我有这个数据集,我正在检查,以确认每种动物的身份是否正确。为此,我使用以下代码在Excel文件中搜索关键字:
do.call(rbind,breeder[-1]) %>%
select(Year, `Old Tag`, Tag_11, PIT, Sex Orig, Group,Comments) %>%
filter(Sex != "m",grepl(keywords, Comments)) %>%
arrange(., desc(PIT)) %>%
print.data.frame以下是关键词:
keywords <- c('retag','lost','Was', 'was','original','change','CHANGE','check','CHECK','switched','temp only','should',
'had tag','new','give','GIVE', 'given','^--', 'tag', 'TAG', 'tags', 'tagged', 'temp', 'Temporarily',
'Temporary', 'Released', 'removed', 'Processing', 'processing', 'Processed', 'previously', 'pit', 'pits',
'PIT', 'orig', 'original', 'old', 'OLD', 'new', 'New', 'not', 'listed', 'last', 'had',
'could', 'Chech', 'assigned')但是,当我运行代码时,R只使用第一个单词--“retag”,然后得到如下输出:
Year Old Tag Tag_11 PIT Sex Orig Group Comments
1 2015 <NA> 367 <NA> f c o Temporary tag - retag as #3
2 2016 <NA> 367 <NA> f c o Temporary tag - retag as #3
Warning message:
In grepl(keywords, Comments) :
argument 'pattern' has length > 1 and only the first element will be used我需要搜索数据框架中所有关键字的注释,如何搜索多个单词?
Update:当我使用以下代码时,输出中没有标识所有参数。我做错了什么?例如,“已发布”不被读取。
deadKeywords <- c('died', 'Released', 'processed', 'Processed', 'processing', 'Processing', 'process', 'dead', 'Dead', 'Died') %>% paste0(., collapse = " | ")
commentSearch <- do.call(rbind,breeder[-1]) %>%
select(Year, Old Tag, Tag_11, PIT, Sex, Orig, Group, Comments) %>%
filter(grepl(deadKeywords, Comments)) %>% arrange(., desc(PIT)) %>%
print.data.frame发布于 2018-05-11 22:42:46
grepl函数的模式没有向量化。为了使模式参数在匹配字符向量中的任何项的意义上被“向量化”,您需要将它们与regex“颇具”的-operator绑定在一起,这样您的模式参数到grepl应该是:
paste0( keywords, collapse="|")另一种使用该方法的方法(如果关键字是一个非常长的向量,则可能有用):
any( sapply( keywords, grepl, x=Comments) )https://stackoverflow.com/questions/50301035
复制相似问题