我正在对状态数据框进行子集化。我想要在列子区域中有“吐”的行。但它给了我所有的NA。我哪里错了?
我最终想要添加另一个这样的列
states$code[states$subregion=="spit"]="Priority1"
示例代码:
states=map_data("state")
states[states$subregion=="spit",]
# long lat group order region subregion
# NA NA NA NA NA <NA> <NA>
# NA.1 NA NA NA NA <NA> <NA>
# NA.2 NA NA NA NA <NA> <NA>
# NA.3 NA NA NA NA <NA> <NA>
# NA.4 NA NA NA NA <NA> <NA>
# NA.5 NA NA NA NA <NA> <NA>
但是这给了我正确的结果states[states$region=="north carolina",]
答案 0 :(得分:2)
查看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),]