[Rd] Modification to cov and cor

Jonathan Rougier J.C.Rougier@durham.ac.uk
Fri, 10 Nov 2000 15:33:51 +0000 (GMT)

Can I suggest that in cov and cor the lines

    if (is.data.frame(x)) 
        x <- as.matrix(x)
    if (is.data.frame(y)) 
        y <- as.matrix(y)

be replaced by 

    if (is.data.frame(x)) 
        x <- data.matrix(x) # coerce logical and factors
    if (is.data.frame(y)) 
        y <- data.matrix(y) # likewise

This would allow things like

> data(iris)
> cor(iris)

which seems like a reasonable thing to do (certainly to my students!).  It
would also give a more informative error message:

> iris$Species <- as.character(iris$Species)
> cor(iris) # modified function
Error in data.matrix(x) : non-numeric data type in frame

as opposed to

> cor(iris) # original function
Error in cor(iris) : missing observations in cov/cor
In addition: Warning message: 
NAs introduced by coercion 

Cheers, Jonathan.

Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE

r-devel 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-devel-request@stat.math.ethz.ch