[R] compounding precipitation based on whether falls within a day
bgunter.4567 at gmail.com
Wed Sep 13 20:06:16 CEST 2017
Thanks for the reprex. Wouldn't have bothered without it.
The following is I believe **almost** what you want. It seems a bit clumsy
to me, so others may provide you something neater. But anyway...
## Convert POSIXct vector to dates
## There are 22 different days, not 21
date <- as.Date(prec_idx)
## Sum results by date at each i,j of the last 2 array dimensions
z <- lapply(unique(date),function(d)
## This gives a list with 22 3x4 matrices of sums.
## Convert to 3x4x22 array with
prec_daily <- array(unlist(z),dim=c(3,4,22))
## This is the **almost** part. You can use the aperm() function to reshape
the array if you like. I leave those pleasures to you.
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Sep 13, 2017 at 9:52 AM, Morway, Eric <emorway at usgs.gov> wrote:
> Using the small reproducible example below, I'm wondering how best to
> complete the following task:
> In the small reproducible example below, the 3D array prec has indexes that
> correspond to time, x, y (i.e., prec[time, x, y]). In this case, the time
> index is hours since some predefined start time. I'd like to add up all
> the time indexes in 'prec' based on whether or not the corresponding hours
> fall within a given day. So, at the end of the small example below, there
> are two variables that I'm left with, prec_idx (an hourly sequence from
> beg_time to end_time) whose length is equal to the first index (the time
> index) of the 3D array of precipitation called prec. That is, I'd like to
> get a 3D array called prec*_daily* that has dimension prec*_daily*[21, 3,
> where 21 is the number of days and the value in say prec*_daily*[1,x,y] is
> equal to prec[1,x,y] + prec[2,x,y] + ... + prec[24,x,y]
> ndays <- 21
> base_time <- as.character('2001-12-31T23:00:00Z')
> hrs_since_base <- 1
> # adding an extra second to the end b/c I'm paranoid about the midnight
> time stamp not being explicit
> beg_time <- as.POSIXct(base_time, format = "%Y-%m-%dT%H:%M:%SZ") +
> (hrs_since_base * 60 * 60) + 1
> max_hr_since <- 24 * ndays
> end_time <- as.POSIXct(base_time, format = "%Y-%m-%dT%H:%M:%SZ") +
> (max_hr_since * 60 * 60) + 1
> prec_idx <- seq(beg_time, end_time, by='hour')
> prec <- array(abs(rnorm((24*ndays) * 3 * 4)) , dim=c(24*ndays, 3, 4))
> # 504
> # 504 3 4
> # How do I aggregate prec to get daily sums of precipitation based on the
> prec_idx array?
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help