periodize | R Documentation |
Periodize datetime object by rounding ceiling or floor to nearest period defined by unit
and amount
.
periodize(idate, itime = 0L, unit, amount = 1L, origin = "1970-01-01", type = "ceiling") as.IPeriod(x, ...) ## Default S3 method: as.IPeriod(x, unit, amount = 1L, origin = "1970-01-01", type = "ceiling", ...) ## S3 method for class 'POSIXct' as.IPeriod(x, unit, amount = 1L, origin = "1970-01-01", type = "ceiling", ...) ## S3 method for class 'data.table' as.IPeriod(x, unit, amount = 1L, origin = "1970-01-01", type = "ceiling", ...) ## S3 method for class 'IDate' as.IPeriod(x, unit, amount = 1L, origin = "1970-01-01", type = "ceiling", itime = 0L, ...) as.factor.IPeriod(x) # do not use without setting `origin`, read notes
idate |
POSIXct, IDate, IDateTime-like data.table, if IDate then also |
itime |
ITime class object. If |
x |
object to be converted to IPeriod, accepted are POSIXct, IDate, IDateTime-like data.table. |
unit |
character scalar, one of secs, mins, hours, days. |
amount |
integer amount of the unit that forms single period. |
origin |
character date format of origin to be used as internal start of integer values. Default 1970-01-01. |
type |
character scalar, one of ceiling, floor, also up, down supported. Default ceiling. |
... |
argument to be matched in methods, e.g. |
Function periodize
is just a wrapper to as.IPeriod
which has to make it handy to use in data.table DT[, .(hourly_mean = mean(x)), .(iperiod = periodize(idate,itime,"hours")]
. It handle reordering unnamed input arguments when x
is POSIXct, data.table by removing itime
from the expected argument sequence, allowing simple periodize(posixct, "hours", 15L)
calls. It also has some defaults.
Integer based IPeriod class. Four attributes attached unit, amount, origin, type.
Use tight origin
argument for potential speed-up when < 100k values (see IDateTime), or in case of using as.factor.IPeriod
because factor levels will start from origin
value, see examples.
Timezone should be handled after extracting date from IPeriod, e.g. as.POSIXct(iperiod, tz="")
. IPeriod data with different attributes should not be compared to each other.
Jan Gorecki
IDateTime, IDate, ITime
# basic usage IPeriod x = as.POSIXct("2015-10-13 09:48:15", tz="UTC") print(x) # rounding ceiling or floor to flexible time periods as.IPeriod(x, "hours", 12) as.IPeriod(x, "mins", 10) as.IPeriod(x, "secs", 15) as.IPeriod(x, "secs", 15, type = "floor") # input args dynamic matching idt = IDateTime(x) # POSIXct periodize(x, "hours", 2) # IDateTime periodize(idt, "hours", 2) # IDate, ITime periodize(idt$idate, idt$itime, "hours", 2) # use to aggregate dataset set.seed(1) DT = data.table(ts = as.POSIXct(1444857558L+3600L*c(1:100)/10, tz="UTC", origin="1970-01-01"), x = rnorm(100)) # group by periodize of idate and itime fields DT[, c("idate","itime") := IDateTime(ts)] DT[, .(hourly_mean = mean(x), hourly_pos = sum(x > 0), hourly_neg = sum(x < 0), range = diff(range(x))), .(iperiod = periodize(idate, itime, "hours"))] # group by periodize directly on POSIXct DT[, .(hourly_mean = mean(x), hourly_pos = sum(x > 0), hourly_neg = sum(x < 0), range = diff(range(x))), .(iperiod = periodize(ts, "hours"))] # extract from IPeriod p = as.IPeriod(x, "hours", 12) as.character(p) as.POSIXct(p, tz="UTC") as.IDate(p) as.ITime(p) IDateTime(p) # don't try `as.factor` without setting `origin`! # origin and as.factor (p = periodize(x, "hours", 6, origin = "2015-10-13")) as.factor.IPeriod(p) (p = periodize(x, "hours", 6, origin = "2015-10-01")) as.factor.IPeriod(p) # input of: "hours", 6, origin = "1970-01-01" (default!) # produces factor of 60k+ levels! # input of: unit="secs", amount=1L # produces 1s periods, which maps to POSIXct based numeric now = Sys.time() attr(now, "tzone") <- "UTC" periodize(now, "secs", type = "floor") == as.integer(now) periodize(now, "secs") == as.integer(now) + 1L