[Rd] cor(x,y) when is.null(y) (PR#883)
cberry@tajo.ucsd.edu
cberry@tajo.ucsd.edu
Fri, 23 Mar 2001 20:34:56 +0100 (MET)
Problem:
> names(my.dat)
[1] "x" "y"
> cor(my.dat$x,my.dat$y) # desired result
[1] 0.8660254
> cor(my.dat$x,my.dat$y.NOT) # typo
[1] 1
>
I'd prefer a warning() or stop() in that case.
Solution (??) :
*** 1,5 ****
--- 1,7 ----
cor <- function (x, y=NULL, use="all.obs")
{
+ if (is.null(y) && !is.null( match.call()$y ) )
+ stop("y evaluated as NULL")
na.method <- pmatch(use, c("all.obs", "complete.obs",
"pairwise.complete.obs"))
if(is.data.frame(x)) x <- as.matrix(x)
if(is.data.frame(y)) y <- as.matrix(y)
then:
> cor(my.dat$x,my.dat$y.NOT)
Error in cor(my.dat$x, my.dat$y.NOT) : y evaluated as NULL
>
> cor(my.dat$x,NULL) # intended NULL still passes
[1] 1
>
> cor(my.dat$x) # default: arg y = NULL
[1] 1
>
Likewise for var() and cov()
Chuck Berry
--please do not edit the information below--
Version:
platform = sparc-sun-solaris2.7
arch = sparc
os = solaris2.7
system = sparc, solaris2.7
status =
major = 1
minor = 2.2
year = 2001
month = 02
day = 26
language = R
Search Path:
.GlobalEnv, package:ctest, Autoloads, package:base
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-0645
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._