# [R] calculating mean matrix

David L Carlson dcarlson at tamu.edu
Sat Jan 19 16:37:48 CET 2013

```I'm not sure I understand your question. It is always better to use an
example:

> set.seed(42)
> dataN <- array(sapply(1:5, function(i) assign(paste0("data",i),
+     matrix(rnorm(6), 2, 3))), c(2, 3, 5))
> meanmtrx <- apply(dataN,1:2,mean)
> meanmtrx
[,1]       [,2]       [,3]
[1,]  0.189669255  0.3368646 0.34261301
[2,] -0.009700353 -0.4676745 0.01974906

The result is a matrix, not a data frame, and certainly not "resulting
data frames." What are you trying to cbind?

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of ya
> Sent: Saturday, January 19, 2013 8:50 AM
> To: r-help
> Subject: [R] calculating mean matrix
>
> Hi list,
>
> Thank you vey much for reading this post.
>
> I have a data frame, I am trying to split it into a couple of data
> frame using one of the columns, say, x. After I get the data frames, I
> am planning to treat them as matrices and trying to calculate an
> element by element mean matrix. Could anyone give me some advice how to
> do it?
>
> So far, I know that if I have a couple of matrices, say
> data1,data2,data3,data4...dataN, I can do it like this:
>
> data=array(cbind(data1,data2,data3,data4,....dataN), c(2, 3, N))
> #2 refers to row number of matrix, 3 refers to column number of matrix,
> N refers to number of matrices to be averaged.
> meanmtrx=apply(data,1:2,mean)
>
> but I do not know how to use the resulting data frames with cbind().
> Maybe there are other better ways. Any advice is appreciated.
>
> Thank you very much.
>
> Have a nice day.
>
> ya
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help