Re: [R] summarizing matrix data

From: Emilio Gagliardi <>
Date: Sun, 02 Mar 2008 13:00:27 -0700


Again, thank you very much for your code snippets, I am learning from them, but they are also creating as many questions as answers...

On Sun, Mar 2, 2008 at 11:48 AM, Henrique Dallazuanna <> wrote:

> Then you can try this:
> apply(array(as.vector(sapply(patt, get)), dim = c(dim(get(patt[1])),
> ngroups, nsubs)), 1:3, mean)
> For create the matrices in a loop one option is:
> lapply(seq(1, 51, by=10), function(x)matrix(seq(x, x+8), nrow=3, ncol=3))

if I use the above code, I get back a list of matrices, which is awesome! The problem is that your code to get the means doesn't work on it ( apply(array(as.vector(...))...additionally, I'm not creating the data, but reading it in, file by file and I don't know what group to assign a file until I open it (group2, group4, group4,group1,group3,group2,group1,etc)

so I have 120 files to open and process and each file can be assigned to 1 of 4 groups(or lists as your code demonstrates)...but I don't know until I open the file which group hence my loop and meta structure approach. So I need to open the file, create a matrix from it (which I have working), store it along with other data from the same group, and then after I've read all the files and associated the data with a group, then I can generate the means per cell.

So I can create 4 lists of matrices, which is fine by me but I don't see how I extend your code snippets to the case of reading the data from files...I currently have 1 "master" matrix which holds all the subject data as I mentioned in my previous post.

data[3,3,1,nsubjects] represents all the subject data in group 1
data[3,3,2,nsubjects] represents all the subject data in group 2
data[3,3,3,nsubjects] represents all the subject data in group 3

how do i pullout just one group's worth of data and apply the mean function to each cell ?
group1 <- get(data[,,1,] ?
group2 <- get(data[,,2,] ?
mean1 <- apply(group1,1:3,mean) # mean per cell, so nine means (3 x 3) for each group...
mean2 <- apply(group2,1:3,mean) # mean per cell, so nine means (3 x 3) for each group...

thanks so much for your patience!

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sun 02 Mar 2008 - 20:05:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 02 Mar 2008 - 21:30:17 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive