尝试按因子级别对数据进行子集时,我无法使复合语句起作用。这是我使用休斯顿机场数据集编制的合成示例:
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搜索没有发现任何东西。我的语法中是否有明显缺失的东西,还是有另一种方法可以使其工作?
答案 0 :(得分:1)
您想要使用
filtered_df <- hflights[hflights$ccfac != "A" & hflights$ccfac != "B", ]
否则,由于这两个条件在逻辑上是不相交的,因此您将获得所有TRUE
个。 (如果ccfac
既不是A
也不是B
,那么它将是真的。如果A
后者是真的,如果它是B
,前者是真的。因此,它是一个重言式。)