Convert an "sts" Object to a Data Frame
sts_tidy.RdThe as.data.frame method for "sts" objects
cbinds the following slots into a wide data frame (one
row per observation time): observed, epoch, state,
alarm, upperbound, populationFrac (as
population), freq, and epochInPeriod.
The tidy.sts function additionally reshapes
multivariate time series into the long format (one row per
time \(\times\) unit), adding a unit column as well as
the useful epoch transformations year,
epochInYear, and date. The tidy format is
particularly useful for regression models or plotting functions.
Usage
tidy.sts(x, ...)
# S3 method for class 'sts'
as.data.frame(x, ..., tidy = FALSE, as.Date = x@epochAsDate)Value
a data frame combining slots of the "sts" input.
For as.data.frame, the number of rows equals the number of time
points, nrow(x), and there are two special columns:
- freq
for weekly or daily data and if
x@epochAsDateisTRUE, the number of samples in the current year, otherwisex@freq.- epochInPeriod
the epoch fraction within the current year,
epochInYear(x)/freq.
For tidy.sts, the resulting data frame has one row for each
combination of epoch and unit, the latter a factor with
levels the colnames(x). It contains the above special columns
plus the following extra time variables:
Examples
data("momo")
momo
momolong <- tidy.sts(momo)
head(momolong, 3)
str(momolong)
momowide <- as.data.frame(momo)
str(momowide, vec.len = 2) # unit-specific columns, like "sts" matrix slots
stopifnot(nrow(momowide) == nrow(momo))
## tidy.sts() is the same as as.data.frame(<sts>, tidy = TRUE)
stopifnot(identical(as.data.frame(momo, tidy = TRUE), momolong))