[R] Subset Rasterbrick by time

Michael Sumner md@umner @end|ng |rom gm@||@com
Tue Jun 19 07:32:25 CEST 2018


On Mon, 18 Jun 2018, 22:09 David Winsemius, <dwinsemius using comcast.net> wrote:

>
>
> > On Jun 18, 2018, at 7:21 AM, Miluji Sb <milujisb using gmail.com> wrote:
> >
> > Dear all,
> >
> > I have a rasterbrick with the date/time information provided which I
> would
> > like to subset by year.
> >
> > However, when I use the following code for sub-setting;
> >
> > new_brick <- subset(original, which(getZ( original ) >=
> as.Date("2000-01-01
> > 10:30:00") & getZ(original ) <= as.Date("2014-12-31 10:30:00")))
> >
> > The date/time information seems to be lost.
> >
>

This is a bug, I tend to extract (getZ) the dates, do the subset logic on
both and restore (setZ).

It takes a bit of learning and practice, good luck. I can't expand more at
the moment. See R-Sig-Geo for more specific discussion forum, and #rstats
on twitter is really good.

Cheers, Mike

> > Furthermore, the class of the date/time seems to be character;
> >
> > ##
> > class(getZ( original ))
> > [1] "character"
> >
> > Is it possible to convert this string to date before sub-setting or
> retain
> > the date/time information after sub-setting?
>
> Yes, it is certainly possible, but why bother? R's Comparison operators
> work on character values so you should be able to do this (if the
> subsetting is syntactically correct:
>
>  new_brick <- subset(original, which(getZ( original ) >= "2000-01-01
> 10:30:00" & getZ(original ) <= "2014-12-31 10:30:00") )
>
>
> As always if you had presented the output of dput(head(original)) assuming
> that head is a meaningful operation on such an object, the demonstration
> would have been possible. An alternate would be to offer a library call to
> a package and then load a relevant example.
>
>
> Best;
> David
> >
> > ### original RasterBrick ###
> > class       : RasterBrick
> > dimensions  : 600, 1440, 864000, 11320  (nrow, ncol, ncell, nlayers)
> > resolution  : 0.25, 0.25  (x, y)
> > extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
> > coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> > data source :
> >
> /work/mm01117/GLDAS_025_deg/daily/gldas_tavg_tmin_tmax_precip_windspd_sphum_daily_1986_2016.nc4
> > names       : X1986.01.01.10.30.00, X1986.01.02.10.30.00,
> > X1986.01.03.10.30.00, X1986.01.04.10.30.00, X1986.01.05.10.30.00,
> > X1986.01.06.10.30.00, X1986.01.07.10.30.00, X1986.01.08.10.30.00,
> > X1986.01.09.10.30.00, X1986.01.10.10.30.00, X1986.01.11.10.30.00,
> > X1986.01.12.10.30.00, X1986.01.13.10.30.00, X1986.01.14.10.30.00,
> > X1986.01.15.10.30.00, ...
> > Date/time   : 1986-01-01 10:30:00, 2016-12-31 10:30:00 (min, max)
> > varname     : v1
> >
> > ### new RasterBrick ###
> > class       : RasterStack
> > dimensions  : 600, 1440, 864000, 5477  (nrow, ncol, ncell, nlayers)
> > resolution  : 0.25, 0.25  (x, y)
> > extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
> > coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> > names       : X2000.01.01.10.30.00, X2000.01.02.10.30.00,
> > X2000.01.03.10.30.00, X2000.01.04.10.30.00, X2000.01.05.10.30.00,
> > X2000.01.06.10.30.00, X2000.01.07.10.30.00, X2000.01.08.10.30.00,
> > X2000.01.09.10.30.00, X2000.01.10.10.30.00, X2000.01.11.10.30.00,
> > X2000.01.12.10.30.00, X2000.01.13.10.30.00, X2000.01.14.10.30.00,
> > X2000.01.15.10.30.00, ...
> >
> > Any help will be greatly appreciated.
> >
> > Sincerely,
> >
> > Milu
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using 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.
>
> ______________________________________________
> R-help using 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.
>
-- 
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]




More information about the R-help mailing list