Skip to content

cor( x, y , method = "spearman" ) incorrect if any( is.na(c( x, y (PR#6448)

2 messages · Charles C. Berry, Peter Dalgaard

#
_                
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    1                
minor    8.1              
year     2003             
month    11               
day      21               
language R
Error in cor(1:3, rep(NA, 3)) : missing observations in cov/cor
[1] 1
I think this would fix cor( ), but have not looked at the C-code for
additional gotcha's:

    if (method != "pearson") {
        Rank <- function(u) if (is.matrix(u)) 
            apply(u, 2, rank)
        else rank(u)
+       x.na <- is.na(x)
        x <- Rank(x)
+       is.na( x ) <- x.na
        if (!is.null(y)) {
+           y.na <- is.na(y)
            y <- Rank(y)
+           is.na(y) <- y.na
        }
    }

Chuck


Charles C. Berry                        (858) 534-2098 
                                         Dept of Family/Preventive Medicine
E mailto:cberry@tajo.ucsd.edu	         UC San Diego
http://hacuna.ucsd.edu/members/ccb.html  La Jolla, San Diego 92093-0717
#
cberry@tajo.ucsd.edu writes:
That one got fixed a while back for r-devel, by using na.last="keep"
in the apply call, which is effectively the same fix. Looks like it
was never filed in r-bugs.