Question

I'm working with a system that outputs data in "R dump" format. For example it might output a three-dimensional array looking like this:

obs <- structure(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24), 
          .Dim=c(2,4,3))

I'm new to R but I would like to use R to inspect marginal summaries of this data. For example, I'd like to see a 2x4 table of mean values averaged across that third dimension.

(If possible, I'd also like to see marginal summaries collapsed down to one dimension, e.g. a row of 4 mean values, each mean being taken over a 2x3 slice of my data.)

I tried summary(obs) which collapses all the dimensions and gives overall stats, and sapply(obs, summary) which doesn't collapse any of the dimensions, just giving a "summary" of each individual datum.

I expect there's a function for what I'm after but I can't find it!

Was it helpful?

Solution

apply works for this:

 apply(obs,1:2,mean)
     [,1] [,2] [,3] [,4]
[1,]    9   11   13   15
[2,]   10   12   14   16

or

aperm(apply(obs,1:2,summary),c(1,3,2))

(or apply(obs,2:1,summary) as pointed out in comments)

with results:

        [,1] [,2] [,3] [,4]
Min.       1    3    5    7
1st Qu.    5    7    9   11
Median     9   11   13   15
Mean       9   11   13   15
3rd Qu.   13   15   17   19
Max.      17   19   21   23

, , 2

        [,1] [,2] [,3] [,4]
Min.       2    4    6    8
1st Qu.    6    8   10   12
Median    10   12   14   16
Mean      10   12   14   16
3rd Qu.   14   16   18   20
Max.      18   20   22   24

As requested you can get other marginal summaries

apply(obs,2,mean)
## [1]  9.5 11.5 13.5 15.5

(double-check: mean(obs[,1,]) is indeed 9.5 ...)

OTHER TIPS

While digging in the R toolbox, you may also wish to check the plyr tool: a*ply. The function takes an array as input, and it is easy to control in which form the result is returned: array, data frame or list.

To make it a little bit easier to keep track of the dimensions when we play around with your example array, I added some arbitrary dimension names. The first dimension (rows) = species; second (columns) = time; third (separate 'tables') = site

obs <- array(c(1:24), 
         dim = c(2, 4, 3),
         dimnames = list(species = c("cat", "dog"),
                         time = 1:4,
                         site = letters[1:3]))

library(plyr)
# result as (2-d) array: aaply
# i.e. same output as @Ben Bolker's `apply` example
# keep the first two dimensions (species, time), collapse the third (site)
aaply(obs, 1:2, mean)

#         time
# species  1  2  3  4
#     cat  9 11 13 15
#     dog 10 12 14 16

# result as data frame: adply
adply(obs, 1:2, mean)

#   species time V1
# 1     cat    1  9
# 2     dog    1 10
# 3     cat    2 11
# 4     dog    2 12
# 5     cat    3 13
# 6     dog    3 14
# 7     cat    4 15
# 8     dog    4 16

# several functions
adply(obs, 1:2, each(min, mean, max))
#   species time min mean max
# 1     cat    1   1    9  17
# 2     dog    1   2   10  18
# 3     cat    2   3   11  19
# 4     dog    2   4   12  20
# 5     cat    3   5   13  21
# 6     dog    3   6   14  22
# 7     cat    4   7   15  23
# 8     dog    4   8   16  24

# apparently the `each` thing can be used on just one function as well,
# then the function name appears as column name instead of 'V1' as above.
adply(obs, 1:2, each(mean))
#   species time mean
# 1     cat    1    9
# 2     dog    1   10
# 3     cat    2   11
# 4     dog    2   12
# 5     cat    3   13
# 6     dog    3   14
# 7     cat    4   15
# 8     dog    4   16

# one-dimensional summary    
adply(obs, 2, each(mean))
#   time mean
# 1    1  9.5
# 2    2 11.5
# 3    3 13.5
# 4    4 15.5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top