This is my first time to ask for help in the R mailing list, so sorry for my 
misbehavior.


The question is actually an example of the apply function embedded in R. Code 
is here:

> x <- cbind(x1 = 3, x2 = c(4:1, 2:5)) > dimnames(x)[[1]] <- letters[1:8] > x   
> x1 x2 a  3  4 b  3  3 c  3  2 d  3  1 e  3  2 f  3  3 g  3  4 h  3  5 > cave 
> <- function(x, c1, c2) c(mean(x[c1]), mean(x[c2])) > apply(x,1, cave,  
> c1="x1", c2=c("x1","x2"))        a b   c d   e f   g h [1,] 3.0 3 3.0 3 3.0 3 
> 3.0 3 [2,] 3.5 3 2.5 2 2.5 3 3.5 4 > x["x1"] [1] NA > mean(x["x1"]) [1] NA > 
> mean(x[c("x1", "x2")]) [1] NA
What confused me is how do apply work to deal with x[c1] and x[c2] (the output 
seems unchanged) ? Why can't I call them out the apply function ?
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to