R中因子水平的复合子集?

时间:2014-03-28 22:04:28

标签: r

尝试按因子级别对数据进行子集时,我无法使复合语句起作用。这是我使用休斯顿机场数据集编制的合成示例:

library(hflights)

# Add a factor column for testing. 
hflights$ccfac <- as.factor(hflights$CancellationCode)
levels(hflights$ccfac)

# Filtering on a single factor level works fine. 
filtered_df <- hflights[hflights$ccfac != "C", ]

# Compound filtering on multiple non-factor items works fine. 
filtered_df <- hflights[hflights$Month == 2 | hflights$Month == 7, ]

# Compound filtering on multiple factor levels doesn't work.
# filtered_df is identical to test_df.
filtered_df <- hflights[hflights$ccfac != "A" | hflights$ccfac != "B", ]

谷歌和StackOverflow搜索没有发现任何东西。我的语法中是否有明显缺失的东西,还是有另一种方法可以使其工作?

1 个答案:

答案 0 :(得分:1)

您想要使用

filtered_df <- hflights[hflights$ccfac != "A" & hflights$ccfac != "B", ]

否则,由于这两个条件在逻辑上是不相交的,因此您将获得所有TRUE个。 (如果ccfac既不是A也不是B,那么它将是真的。如果A后者是真的,如果它是B,前者是真的。因此,它是一个重言式。)