Learn R Programming

tsibble (version 0.5.2)

as_tsibble: Coerce to a tsibble object

Description

Coerce to a tsibble object

Usage

as_tsibble(x, ...)

# S3 method for tbl_df as_tsibble(x, key = id(), index, regular = TRUE, validate = TRUE, ...)

# S3 method for tbl_ts as_tsibble(x, validate = FALSE, ...)

# S3 method for data.frame as_tsibble(x, key = id(), index, regular = TRUE, validate = TRUE, ...)

# S3 method for list as_tsibble(x, key = id(), index, regular = TRUE, validate = TRUE, ...)

# S3 method for ts as_tsibble(x, tz = "UTC", ...)

# S3 method for mts as_tsibble(x, tz = "UTC", gather = TRUE, ...)

# S3 method for msts as_tsibble(x, tz = "UTC", gather = TRUE, ...)

# S3 method for hts as_tsibble(x, tz = "UTC", ...)

Arguments

x

Other objects to be coerced to a tsibble (tbl_ts).

...

Other arguments passed on to individual methods.

key

Structural variable(s) that define unique time indices, used with the helper id. If a univariate time series (without an explicit key), simply call id(). See below for details.

index

A bare (or unquoted) variable to specify the time index variable.

regular

Regular time interval (TRUE) or irregular (FALSE). The interval is determined by the greatest common divisor of positive time distances, if TRUE.

validate

TRUE suggests to verify that each key or each combination of key variables lead to unique time indices (i.e. a valid tsibble). It will also make sure that the nested variables are arranged from lower level to higher, if nested variables are passed to key. If you are sure that it's a valid input, specify FALSE to skip the checks.

tz

Time zone. May be useful when a ts object is more frequent than daily.

gather

TRUE gives a "long" data form, otherwise as "wide" as x.

Value

A tsibble object.

See Also

tsibble

Examples

Run this code
# NOT RUN {
# coerce tibble to tsibble w/o a key ----
tbl1 <- tibble(
  date = seq(as.Date("2017-01-01"), as.Date("2017-01-10"), by = 1),
  value = rnorm(10)
)
as_tsibble(tbl1)
# specify the index var
as_tsibble(tbl1, index = date)

# coerce tibble to tsibble with one key ----
# "date" is automatically considered as the index var, and "group" is the key
tbl2 <- tibble(
  mth = rep(yearmonth(seq(2017, 2017 + 9 / 12, by = 1 / 12)), 3),
  group = rep(c("x", "y", "z"), each = 10),
  value = rnorm(30)
)
as_tsibble(tbl2, key = id(group))
as_tsibble(tbl2, key = id(group), index = mth)

# coerce ts to tsibble
as_tsibble(AirPassengers)
as_tsibble(sunspot.year)
as_tsibble(sunspot.month)
as_tsibble(austres)

# coerce mts to tsibble
z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
as_tsibble(z)
as_tsibble(z, gather = FALSE)

# coerce hts from the "hts" package to tsibble
if (!requireNamespace("hts", quietly = TRUE)) {
  stop("Please install the hts package to run these following examples.")
}
as_tsibble(hts::htseg1)
as_tsibble(hts::htseg2)
# }

Run the code above in your browser using DataLab