删除与R中的某些值正确匹配的行

时间:2014-02-26 00:10:48

标签: r filter

我正在尝试删除与R中某个值匹配的记录\行。

这就是我现在所拥有的:

RawSurvey <- read.csv("/Survey/RawSurvey.csv")

Country = RawSurvey[,132] #132nd variable is Country

Survey <- RawSurvey[which(Country != 'Israel' 
                      & Country != 'Canada'
                      & Country != 'Ukraine'
                      & Country != 'Afghanistan'
                      & Country != 'Oman'
                      & Country != 'Argentina'
                      ),]

我不认为它会丢掉记录 - 我的意思是当我做总结时(Survey $ Country)他们仍然出现但是0表示阿根廷 - 0。

1 个答案:

答案 0 :(得分:3)

使用%in%可以节省大量复杂性,例如

countriesToDrop<-c("Israel","Canada","Ukraine","Afghanistan","Oman","Argentina");
Survey<-RawSurvey[!(RawSurvey[,132]%in%countriesToDrop),]

下降国家出现0计数的事实是因子向量的正常预期行为;如果您愿意,可以通过重新计算因子水平来删除它们,如下所示:

Survey[,132]<-factor(Survey[,132])