查看 的结构并states
特别注意NA
. subregion
如果您像您正在做的那样使用方括号进行子集化,NA
则包含在子集中。一个小例子:
dat <- data.frame(x=1:10, y=c('a', 'b'), z='foo')
dat$y[9:10] <- NA
dat[dat$y == 'a',]
x y z
1 1 a foo
3 3 a foo
5 5 a foo
7 7 a foo
NA NA <NA> <NA>
NA.1 NA <NA> <NA>
如果你使用na.omit
或类似的东西,你会看到你期望的结果:
foo <- states[states$subregion=="spit",]
foo[!is.na(foo$subregion),]
# long lat group order region subregion
# 10321 -76.00285 36.55471 40 10321 north carolina spit
# 10322 -75.93983 36.55471 40 10322 north carolina spit
# 10323 -75.92837 36.54898 40 10323 north carolina spit
# 10324 -75.91691 36.52606 40 10324 north carolina spit
# 10325 -75.91118 36.50887 40 10325 north carolina spit
# 10326 -75.91118 36.49168 40 10326 north carolina spit
# 10327 -75.93983 36.49168 40 10327 north carolina spit
# 10328 -75.96848 36.49741 40 10328 north carolina spit
# 10329 -75.97421 36.50314 40 10329 north carolina spit
# 10330 -75.97421 36.52606 40 10330 north carolina spit
# 10331 -75.98566 36.55471 40 10331 north carolina spit
# 10332 -76.00285 36.55471 40 10332 north carolina spit
或者在一行中:
states[states$subregion == 'spit' & !is.na(states$subregion),]