2

I have a netcdf file with daily data and I calculate for each week some statistics, using the "apply.weekly" command in the "xts" library. However, I would like to calculate bi-weekly statistics, using daily data from every two weeks as input. How could i do this in R?

library("ncdf4")
library("rts")
library("xts")
#
prec = stack("pr.nc")
#
prec
#
class       : RasterStack 
dimensions  : 12, 19, 228, 365  (nrow, ncol, ncell, nlayers)
resolution  : 0.125, 0.125  (x, y)
extent      : 21.75, 24.125, 39.875, 41.375  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
names       :  X2018.01.01,  X2018.01.02,  X2018.01.03,  X2018.01.04,  X2018.01.05,  X2018.01.06,  X2018.01.07,  X2018.01.08,  X2018.01.09,  X2018.01.10,  X2018.01.11,  X2018.01.12,  X2018.01.13,  X2018.01.14,  X2018.01.15, ... 
min values  : 1.062151e-13, 1.080165e-13, 1.061332e-13, 1.757816e-02, 4.101618e-02, 3.535164e-01, 2.539131e-02, 2.674782e-02, 1.932025e-02, 2.027835e-05, 2.576398e-06, 6.366093e-13, 7.227548e-05, 1.345976e-04, 2.202405e-08, ... 
max values  :   7.53125000,   0.69336098,   0.09375487,   9.00326061,  23.77886200,  16.37112999,   6.83587980,   4.03569651,  21.04327393,   6.23503065,   3.15138841,   1.24927104,   4.46353579,   3.46504283,   4.42247295, ... 
#
indices = seq.Date(min(as.Date("2018-1-1")), max(as.Date("2018-12-31")), by="day")
#
prec_rt = rts(prec, indices) 
#
rr_2018_weekly_sum = apply.weekly(prec_rt, sum)
  • use period.apply() to specify periods other than days, weeks, months – JepsonNomad Mar 29 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.