2

次の形式のデータ フレームがあります。

>df
stationid    station      gear sample     lat    lon       date depth
1     25679          CORBOX150    UE4 53.9015 7.8617 15.07.1987    19
2     25681 UE9 Kern CORCRB050    UE9 54.0167 7.3982 15.07.1987    33
3        NA                           54.0167 7.3982 15.07.1987    33

の論理テスト stationidにより、正しい最初の行の隣に、NA でいっぱいの迷惑な行が表示されます。

> df[df$stationid=="25679",]
stationid station      gear sample     lat    lon       date depth
1      25679         CORBOX150    UE4 53.9015 7.8617 15.07.1987    19
NA        NA    <NA>      <NA>   <NA>      NA     NA       <NA>    NA

何故ですか?

の 3 行目のどこかでdf、おかしくなっていると思います。

データは次のとおりです。

df<-structure(list(stationid = c(25679L, 25681L, NA), station = structure(c(2L, 
3L, 1L), .Label = c("", " ", "UE9 Kern"), class = "factor"), 
gear = structure(c(2L, 3L, 1L), .Label = c("", "CORBOX150", 
"CORCRB050"), class = "factor"), sample = structure(c(2L, 
3L, 1L), .Label = c("", "UE4", "UE9"), class = "factor"), 
lat = c(53.9015, 54.0167, 54.0167), lon = c(7.8617, 7.3982, 
7.3982), date = structure(c(1L, 1L, 1L), .Label = "15.07.1987", class = "factor"), 
depth = c(19L, 33L, 33L)), .Names = c("stationid", "station", 
"gear", "sample", "lat", "lon", "date", "depth"), class = "data.frame", row.names = c(NA, 
-3L))
4

2 に答える 2

2

との比較は、 ( http://cran.r-project.org/doc/manuals/R-intro.html#Missing-valuesNAを参照)の結果につながります ... どちらかを使用できますNA

df[df$stationid==25679 & !is.na(df$stationid),]

または(上記のコメントで提案されているように)

df[which(df$stationid==25679),] 

また

subset(df,stationid==25679)

(値subsetを削除するという望ましくない副作用が時々ありNAますが、この場合はまさにあなた望むものです)

于 2012-08-24T15:24:28.637 に答える
1

別の解決策はdf[df$stationid==25679 & !is.na(df$stationid),]. はるかに長いですが、より明確です。

于 2012-08-24T15:24:06.967 に答える