[R] summarizing matrix data

Henrique Dallazuanna wwwhsd at gmail.com
Sun Mar 2 19:48:30 CET 2008


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))



On 02/03/2008, Emilio Gagliardi <emilio at ualberta.ca> wrote:
> Hi Henrique,
>
> Thank you for your help, but it doesn't produce what I'm looking for...
>
> On Sun, Mar 2, 2008 at 10:50 AM, Henrique Dallazuanna <wwwhsd at gmail.com>
> wrote:
>
> > One options is:
> >
> > data.1 <- matrix(seq(from=1,to=9,by=1),nrow=3,ncol=3)
> > data.2 <- matrix(seq(from=11,to=19,by=1),nrow=3,ncol=3)
> > data.3 <- matrix(seq(from=21,to=29,by=1),nrow=3,ncol=3)
> > data.4 <- matrix(seq(from=31,to=39,by=1),nrow=3,ncol=3)
> > data.5 <- matrix(seq(from=41,to=49,by=1),nrow=3,ncol=3)
> > data.6 <- matrix(seq(from=51,to=59,by=1),nrow=3,ncol=3)
> >
> > patt <- ls(patt='^data\\.[0-9]')
> > nsubs <- 2
> > ngroups <- 3
> > apply(array(as.vector(sapply(patt, get)), dim = c(dim(get(patt[1])),
> > ngroups, nsubs)),          3, mean)
>
>
> generated [1] 20 30 40
>
> but what I need is an average per cell...so in the above, there are 6 (3 x
> 3) matrices across 3 groups ( 2 subjects per group)..what I need is 1 matrix
> per group, not one mean.  Moreover, I'm storing the data in a single
> multi-dimensional array, not as a collection of arrays since I'm creating
> each data matrix within a loop (I read a file, create the data matrix, store
> it in a larger structure) so that I can perform operations on all the data
> outside of the loop...
>
> for(i in 1:6)
> {
>      str <- paste("test",i,sep=".")
>      str <- matrix(seq(from=1,to=9,by=1),nrow=3,ncol=3) # works, creates 6
> matrices
> }
> pattern <- ls(patt='^test\\.[0-9]') # doesn't work variables only exist in
> the loop
>
> which is why I had:
> subject.data[,,1,1] <- data.1               # subject 1 group 1
> subject.data[,,2,1] <- data.2               # subject 1 group 2
> subject.data[,,3,1] <- data.3               # subject 1 group 3
> subject.data[,,3,2] <- data.4               # subject 2 group 3
> subject.data[,,2,2] <- data.5               # subject 2 group 2
> subject.data[,,1,2] <- data.6               # subject 2 group 1
>
> to demonstrate all the data was in a meta structure so that I could access
> them later...am I making this more complicated than it needs to be? How can
> I create the matrices inside a loop and have them available outside of it?
>
> thanks again,
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list