timeid | R Documentation |
timeid
groups time vectors in a way that preserves the temporal structure. It generate an integer id where unit steps represent the greatest common divisor in the original sequence e.g c(4, 6, 10) -> c(1, 2, 4)
or c(0.25, 0.75, 1) -> c(1, 3, 4)
.
timeid(x, factor = FALSE, ordered = factor, extra = FALSE)
x |
a numeric time object such as a |
factor |
logical. |
ordered |
logical. |
extra |
logical.
Note that returning these attributes does not incur additional computations. |
Let range_x
and step_x
be the like-named attributes returned when extra = TRUE
, then, if factor = TRUE
, a complete sequence of levels is generated as seq(range_x[1], range_x[2], by = step_x) |> copyMostAttrib(x) |> as.character()
. If factor = FALSE
, the number of timesteps recorded in the "N.groups"
attribute is computed as (range_x[2]-range_x[1])/step_x + 1
, which is equal to the number of factor levels. In both cases the underlying integer id is the same and preserves gaps in time. Large gaps (strong irregularity) can result in many unused factor levels, the generation of which can become expensive. Using factor = FALSE
(the default) is thus more efficient.
A factor or 'qG
' object, optionally with additional attributes attached.
seqid
, Indexing, Time Series and Panel Series, Collapse Overview
oldopts <- options(max.print = 30)
# A normal use case
timeid(wlddev$decade)
timeid(wlddev$decade, factor = TRUE)
timeid(wlddev$decade, extra = TRUE)
# Here a large number of levels is generated, which is expensive
timeid(wlddev$date, factor = TRUE)
tid <- timeid(wlddev$date, extra = TRUE) # Much faster
str(tid)
# The reason for step = 1 are leap years with 366 days every 4 years
diff(attr(tid, "unique"))
# So in this case simple factor generation gives a better result
qF(wlddev$date, ordered = TRUE, na.exclude = FALSE)
# The best way to deal with this data would be to convert it
# to zoo::yearmon and then use timeid:
timeid(zoo::as.yearmon(wlddev$date), factor = TRUE, extra = TRUE)
options(oldopts)
rm(oldopts, tid)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.