[R] Novice programing question

Ista Zahn istazahn at gmail.com
Thu Oct 25 03:35:18 CEST 2007


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



More information about the R-help mailing list