[R] colSums in C
David Brahm
brahm at alum.mit.edu
Wed Dec 19 01:06:17 CET 2001
Peter Dalgaard <p.dalgaard at biostat.ku.dk> writes:
> [isnan() is] not logically doing the same thing [as ISNA].
> NA is an NaN, but you can also get NaN from 0/0 like construction, and you
> may want the sum to be NaN in that case.
Luckily, isnan() seems to give nearly S-Plus-compatible results. Here's an
S-Plus session:
S> x <- matrix(1:15, 3,5, dimnames=list(letters[1:3], LETTERS[1:5]))
S> x[1,1] <- x[1,3] <- x[ ,4] <- NA
S> x[2,2] <- x[2,3] <- 0/0
S> print(y <- colSums(x))
A B C D E
NA NA NA NA 42
S> is.na(y)
A B C D E
T T T T F
S> is.nan(y)
[1] F T F F F
S> colSums(x, na.rm=T)
A B C D E
5 10 9 0 42
and in R I get the same results (different printing) except for this line:
R> is.nan(y)
A B C D E
FALSE TRUE TRUE FALSE FALSE # (Note column C is different!)
Three points about colSums to glean from this:
1) In S-Plus, na.rm=T removes (sets to 0) NaN's as well as NA's.
2) A column full of NA's sums to 0 when na.rm=T.
3) In S-Plus, NaN + NA = NA, but in R (as I've written it) NaN + NA = NaN.
I'm OK with that, so I think I'll keep isnan() (or its portable equivalent).
> There is also a portability issue in the availability of isnan(). R
> jumps through a few hoops to ensure that R_IsNA does sensible things
> on as many systems as possible, although we may be running out of IEEE
> non-compatible systems these days.
Brian Ripley <ripley at stats.ox.ac.uk> also mentioned portability:
> Portability. Not all possible ports of R have [isnan()], and R can be built
> without it. But there are macros to cover that.
Is there a macro that is equivalent to (as fast as) isnan() when it exists?
ISNAN is definitely slower.
--
-- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list