gpt4 book ai didi

r - 对数据框属性的逻辑测试如何导致 NA 行

转载 作者:行者123 更新时间:2023-12-04 05:34:39 27 4
gpt4 key购买 nike

我有一个如下形式的数据框:

>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

为什么?

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))

最佳答案

NA 的任何比较导致 NA 的结果(见 http://cran.r-project.org/doc/manuals/R-intro.html#Missing-values )......你可以使用

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

或(如上面评论中所建议的)
df[which(df$stationid==25679),] 


subset(df,stationid==25679)

( subset 有时会产生不必要的副作用,即删除 NA 值,但在这种情况下,这正是您想要的)

关于r - 对数据框属性的逻辑测试如何导致 NA 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12112184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com