0

我正在对状态数据框进行子集化。我想要列子区域中“吐出”的行。但它给了我所有的 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",]

4

1 回答 1

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),]
于 2013-02-14T20:37:27.027 回答