gpt4 book ai didi

r - is.nan() 行为

转载 作者:行者123 更新时间:2023-12-02 05:26:44 26 4
gpt4 key购买 nike

创建了一个数据框:

simpleDF<- structure(list(vals = c(NA, NaN, 2)), .Names = "vals", row.names = c(NA, 
-3L), class = "data.frame")


> is.na(simpleDF$vals)
[1] TRUE TRUE FALSE
> is.nan(simpleDF$vals)
[1] FALSE TRUE FALSE

现在,当我更改数据框以包含字符串值时:

simpleDF <- structure(list(vals = structure(c(NA, 2L, 1L, 3L), .Label = c("2", 
"NaN", "test"), class = "factor")), .Names = "vals", row.names = c(NA,
-4L), class = "data.frame")

> is.na(simpleDF$vals)
[1] TRUE FALSE FALSE FALSE
> is.nan(simpleDF$vals)
[1] FALSE FALSE FALSE FALSE

我不明白为什么 NaN 不再被 is.nan() 识别?当然会有一个解释..

最佳答案

NaN 是一个仅在数字向量中有意义的值,因此当它所在的向量类是(显式或隐式)从数字转换为字符或因子。

vals
# [1] NA NaN 2
as.character(vals)
# [1] NA "NaN" "2"
c(vals, "A")
# [1] NA "NaN" "2" "A"

关于r - is.nan() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12980095/

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