[R] Find mean of values in three-dimensional array

peter dalgaard pdalgd at gmail.com
Wed Jun 15 21:05:12 CEST 2016


> On 15 Jun 2016, at 19:37 , Nick Tulli <nick.tulli.95 at gmail.com> wrote:
> 
> Hey R-Help,
> 
> I've got a three dimensional array which I pulled from a netcdf file.
> The data in array are the humidity values of locations in the United
> States over a time period. The three dimensions are [longitude,
> latitude, days], 141x81x92. My goal is to find the mean value at each
> longitude/latitude over the 92 day period.
> 
> I could probably accomplish my goal by running a loop, but I'm sure
> that there is a much easier and more efficient way to accomplish the
> goal in R. Any suggestions?

Dunno about fast, but the canonical way is apply(A, c(1,2), mean)

E.g.

(A <- array(1:24,c(2,3,4)))
apply(A, c(1,2), mean)
apply(A, c(1,3), mean)

-pd

> 
> 
> Thanks guys.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list