I need to reduce length (generalize) an array in R. For example, I have hi-resolution data like this...
my_array=array(c(sample(0:9,32, replace=TRUE)), dim=c(4,4,2))
> my_array
, , 1
[,1] [,2] [,3] [,4]
[1,] 2 1 8 2
[2,] 3 5 4 6
[3,] 2 8 9 6
[4,] 1 0 9 9
, , 2
[,1] [,2] [,3] [,4]
[1,] 3 7 9 7
[2,] 9 4 9 8
[3,] 8 6 7 8
[4,] 7 6 9 9
...and I need to "generalise" it to low-resolution using the mean function like this:
, , 1
[,1] [,2]
[1,] 2.75 4.00
[2,] 2.75 8.25
, , 2
[,1] [,2]
[1,] 5.75 8.25
[2,] 6.75 8.25
Simply, 4 values of the original array (positions [1,1];[1,2];[2,1];[2,2]) form 1 value (average) in resulting array in [1,1] position. I have tried using "apply" over the array, but I am not able to cope with "non-standard" margins. Is there any more complex function like apply in R?