Reputation: 9794
Created a data frame:
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
Now, when I change the data frame to include a string value:
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
I did not understand why NaN
is no longer recognized by is.nan()
? Sure there would be an explanation ..
Upvotes: 2
Views: 132
Reputation: 162321
NaN
is a value that only makes sense in numeric vector, so it gets converted to the character string "NaN"
when the class of the vector it is in is (explicitly or implicitly) converted from numeric to character or factor.
vals
# [1] NA NaN 2
as.character(vals)
# [1] NA "NaN" "2"
c(vals, "A")
# [1] NA "NaN" "2" "A"
Upvotes: 5