# Re: [R] summarizing matrix data

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Sun, 02 Mar 2008 14:50:18 -0300

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

On 02/03/2008, Emilio Gagliardi <emilio_at_ualberta.ca> wrote:
> Hi everyone,
>
> I'm sure this is simple, but I can't seem to figure this out.
>
> Situation. 3 different groups of subjects each submit n X n matrices of
> scores. What I want to do is aggregate each group of scores into a summary
> n X n matrix. I need the result to be a matrix so that I can calculate a
> dissimilarity structure on it. So I thought I would create a
> multi-dimensional array and store the data that way.
>
> The first two dimensions represent the data, the third the group membership
> (say 3 groups), and forth represents subjects(30 subjects per group)...
>
> subject.data <- array(NA, dim=c(3,3,3,10))
>
> 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)
>
> 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
>
> what I want is conceptually:
> array.group1 <- apply(subject.data[,,1,],mean)
> array.group2 <- apply(subject.data[,,2,],mean)
> array.group3 <- apply(subject.data[,,3,],mean)
>
> each of the summary matrices then contain the mean for each cell across all
> subjects within a group so that it has the same size as the subject data.
> Also, after I've populated subject.data, there will be segments that will
> only contain NAs, how do you grab just the portions of the array that
> contain data? I know the function is.na() will get me an index I just don't
> know how to write it correctly. something like
> only.data <- subject.data[!is.na(subject.data)]
>
> Does that make? I would appreciate any help regarding a better way to store
> the data and obviously how to get back these summary matrices...
>
> Thanks very much for your help.
>
> emilio
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help