2013-02-14 65 views
0

我是子集狀態數據框。我想要列分區域中有「吐」的行。但它給了我所有的NAs。我哪裏錯了?方括號子集賦予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",]

回答

2

看看的states結構和特別要注意的是subregion中有NA的值。如果你像使用方括號一樣使用方括號,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),] 
+0

哎呀,謝謝......這就是我的意思! – Justin 2013-02-14 21:27:16

+0

'長LAT組順序區subregion' '2391 -81.29125 31.41528 10 2391格魯吉亞'' 6081 -70.10712 42.02073 21 6081馬薩諸塞main' '13460 -73.41881 44.13494 52 13460佛蒙特州' 我得到這樣的事情,如果我用na.omit – 2013-02-14 23:22:17

+0

這不能解決我的問題。 – 2013-02-14 23:24:39