[R] Calculate Mean of Column Vectors?

Tim F Liao tfliao at uiuc.edu
Tue Jan 11 05:08:53 CET 2005


Tom,

I often use a loop, such as:

>m <- rep(0,1000)
> for (i in 1:1000)
+ m[i]<-mean(y[,i])
> m[1:20]
 [1] -0.04914724 -0.28253861 -0.31112690 -0.18034371 
0.18839167  0.66448244
 [7]  0.19769017 -1.28363405 -0.05167451 -0.95492534
-1.23285174  0.10288562
[13] -0.73792584 -0.19297468 -0.59059036 -0.11870173 
0.38285449  1.19154411
[19]  0.34663980  0.21322554

But there may be more efficient ways to accomplish this.

Tim


---- Original message ----
>Date: Mon, 10 Jan 2005 22:45:56 -0500
>From: Thomas Hopper <thopper106035 at comcast.net>  
>Subject: [R] Calculate Mean of Column Vectors?  
>To: r-help at stat.math.ethz.ch
>
>Hello,
>
>I've got an array defined as y <- rnorm(3000), dim(y) <- c(3,
1000).
>
>I'd like to produce a 1000-element vector z that is the mean
of the 
>corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], 
>y[3,1])), but being new to R, I'm not sure how to do this for
all 
>elements at once (or, at least, simply). Any help is appreciated.
>
>Thanks,
>
>Tom
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list