# [R] summarizing matrix data

From: Emilio Gagliardi <emilio_at_ualberta.ca>
Date: Sun, 02 Mar 2008 10:24:40 -0700

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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 02 Mar 2008 - 17:29:42 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 - 18:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.