Search code examples
rarraysmultidimensional-arraystatisticsmean

Average for a complex array


For the object below (polysst), how can I take the average of each "slice" of the sea_surface_temperature array?

> str(polysst)
List of 6
 $ sea_surface_temperature: num [1:73, 1:25, 1:384] NA NA NA NA NA NA NA NA NA NA ...
 $ datasetname            : chr "erdPH53sstdmday"
 $ longitude              : num [1:73(1d)] -127 -127 -127 -127 -127 ...
 $ latitude               : num [1:25(1d)] 46 46 46.1 46.1 46.1 ...
 $ altitude               : logi NA
 $ time                   : POSIXlt[1:384], format: "1990-01-17" "1990-02-15" "1990-03-17" "1990-04-16" ...
 - attr(*, "class")= chr [1:2] "list" "rxtracto3D"

> str(polysst$sea_surface_temperature)
 num [1:73, 1:25, 1:384]

> class(polysst$sea_surface_temperature)
[1] "array"

polysst$sea_surface_temperature is a 3*3 array. This represents a spatial grid that is 73 * 25 grid cells, for each of 384 dates (polysst$time). Every value is the sea surface temperature of that grid cell on that date. I am trying to take the average of each 73 * 25 array/matrix, for each of the 384 dates. In effect, this will return the average temperature across the entire spatial grid on that date. I can do this for a single "slice" with this code:

test8 <- mean(as.numeric(polysst$sea_surface_temperature[,,1]), na.rm = TRUE)

How can I do this for the whole array? It would be good if we could preserve the date as well, just to make sure that the list didn't get scrambled in the process. So, a data frame with date and average value is what I'm after.

I'm new to this sort of complex array / object, so I'm hoping that I've missed something obvious like mean(polysst$sea_surface_temperature, by = date)...

Thanks :)


Solution

  • Alright, I've finally solved, posting here incase anyone else is having the same problem.

    test63 <- apply(polysst$sea_surface_temperature, 3, mean, na.rm = TRUE)
    

    This doesn't preserve date, but a quick spot check confirmed that it is in chronological order, so I can just add it to a data frame with the dates in order.