Skip to content
Prev 127834 / 398500 Next

Novice programing question

?var points out that "var is another interface to cov", i.e. it
calculates (possibly part of) the "variance" matrix.  In the simplest
special case y = x and you get the entire variance matrix, or just the
sample variance if x is a simple vector, as in your case.

BTW you little function is a bit more complicated than it need be, isn't
it?
would do the same job with many fewer parentheses.... 


Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables at csiro.au
http://www.cmis.csiro.au/bill.venables/ 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Ista Zahn
Sent: Thursday, 25 October 2007 11:35 AM
To: r-help at r-project.org
Subject: [R] Novice programing question

Hi all,
I apologize for the ignorance implicit in this question, but I'm  
having a hard time figuring out how R functions work. For example, if  
I wanted to write a function to compute a variance, I would do  
something like

 >my.var <- function(x) (sum(((x-mean(x)))^2))/(length(((x-mean(x))) 
^2)-1)

And this seems to work, e.g.,

 > my.var(V1)
[1] 116.1
 > var(V1)
[1] 116.1

But when I try to see what the built-in var function does I get

 > var
function (x, y = NULL, na.rm = FALSE, use)
{
     if (missing(use))
         use <- if (na.rm)
             "complete.obs"
         else "all.obs"
     na.method <- pmatch(use, c("all.obs", "complete.obs",  
"pairwise.complete.obs"))
     if (is.data.frame(x))
         x <- as.matrix(x)
     else stopifnot(is.atomic(x))
     if (is.data.frame(y))
         y <- as.matrix(y)
     else stopifnot(is.atomic(y))
     .Internal(cov(x, y, na.method, FALSE))
}
<environment: namespace:stats>

Being a novice, I can't understand what this means. I only have one  
variable, yet the code seems to be based on the covariance between x  
and y. What is y? Sorry for such a stupid question. I am just trying  
to figure out how R does things, and I can't seem to get my head  
around it. Thank you for your patience.

-Ista Zahn
http://izahn.homedns.org

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.