[R] making spearman correlation cor() call fail with log(0) as input
Timur Shtatland
tshtatland at gmail.com
Wed Sep 10 22:04:23 CEST 2008
Hi,
How can I make the cor(x, y, method="spearman") call to produce an
error when the input to it (x, y) produces an error? Here is a simple
example:
> a <- c(0, 1, 2)
> b <- c(100, 2, 4)
## error:
> log(a)
[1] -Inf 0.0000000 0.6931472
## error, as expected:
> cor(log(a), log(b), method="pearson")
[1] NaN
## not an error any more (not expected):
> cor(log(a), log(b), method="spearman")
[1] -0.5
> cor(log(a), log(b), method="spearman", use="all.obs")
[1] -0.5
> sessionInfo()
R version 2.6.1 (2007-11-26)
i686-pc-linux-gnu
locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
>
Thank you for your help.
Best regards,
Timur Shtatland
More information about the R-help
mailing list