我有以下形式的数据帧:如何对数据框属性逻辑测试导致NA-行
>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
逻辑测试给我,旁边的正确的第一线,一个恼人的线路全程的NAs:
> 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
这是为什么?
df
第3行的某处,我猜想事情会搞砸。
继承人的数据:
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))
这是因为你在'stationid'列有'NA',使用'which'。这个'df [which(df $ stationid ==“25679”)]''应该可以工作 – dickoa