Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

r - NaN is removed when using na.rm=TRUE

This reproducible example is a very simplified version of my code:

x <- c(NaN, 2, 3)

#This is fine, as expected
max(x)
> NaN

#Why does na.rm remove NaN?
max(x, na.rm=TRUE) 
> 3

To me, NA (missing value) and NaN (not a number) are two completely different entities, why does na.rm remove NaN? How can I ignore NA and not NaN?

ps:I am using 64-bit R version 3.0.0 on Windows7.

Edit: Upon some more study I found that is.na returns true for NaN too! This is the cause of confusion for me.

is.na(NaN)
> TRUE
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's a language decision:

> is.na(NaN)
[1] TRUE

is.nan differentiates:

> is.nan(NaN)
[1] TRUE
> is.nan(NA)
[1] FALSE

So you may need to call both.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...