2015-10-20 52 views
2

我有一个数据帧rawdata,其中包含包含生态信息的列。我试图消除列LatinName与我已经拥有一些数据的物种的向量匹配的所有行,并仅创建一个仅包含缺少数据的物种的新数据帧。所以,我想要做的是这样的:消除与字符串匹配的数据帧行

matches <- c("Thunnus thynnus", "Balaenoptera musculus", "Homarus americanus") 
# obviously these are a random subset; the real vector has ~16,000 values 
rawdata_missing <- rawdata %>% filter(LatinName != "matches") 

这是行不通的,因为布尔运算符不能应用于字符串。我也可以做这样的事情:

rawdata_missing <- filter(rawdata, !grepl(matches, LatinName) 

这不起作用,或者是因为!grepl也不能使用的字符串。

我知道有很多的方法,我可以用其中LatinName IS在matches行子集rawdata,但我不能想出一个巧妙的方法进行子集rawdata这样LatinName没有在matches

在此先感谢您的帮助!

+1

就否定了'%在%'运营商 - !'RAWDATA%>%滤波器((LatinName %in%matches))' – thelatemail

+0

@ thelatemail的方法是这里的方法。但是为了将来的参考,如果你确实需要将一个字符串向量转换为一个正则表达式,你可以使用'grepl'或'grep'来使用,例如'match.string = paste(matches,collapse =“|”) )'。 – eipi10

+0

@thelatemail是完美的!谢谢。我只是不知道如何编写否定操作。 – AFH

回答

2
filteredData <- rawdata[!(rawdata$LatinName %in% Matches), ] 
+0

谢谢!这与如上所示否定%中的%一样起作用:) – AFH

0

的另一种方法,通过使用子集,粘贴,mapply和grepl是...

fileteredData <- subset(rawdata,mapply(grepl,rawdata$LatinName,paste(Matches,collapse = "|")) == FALSE)