Learn R Programming

tsibble (version 0.8.6)

as_tsibble: Coerce to a tsibble object

Description

stable

Usage

as_tsibble(
  x,
  key = NULL,
  index,
  regular = TRUE,
  validate = TRUE,
  .drop = TRUE,
  ...
)

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

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

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

Arguments

x

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

key

Unquoted variable(s) that uniquely determine time indices. NULL for empty key, and c() for multiple variables. It works with tidy selector (e.g. dplyr::starts_with()).

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 index column, if TRUE.

validate

TRUE suggests to verify that each key or each combination of key variables leads to unique time indices (i.e. a valid tsibble). If you are sure that it's a valid input, specify FALSE to skip the checks.

.drop

If TRUE, empty key groups are dropped.

...

Other arguments passed on to individual methods.

tz

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

pivot_longer

TRUE gives a "longer" form of the data, otherwise as is.

gather

defunct Please use pivot_longer instead.

Value

A tsibble object.

See Also

tsibble

Examples

Run this code
# NOT RUN {
# coerce tibble to tsibble w/o a key
tbl1 <- tibble(
  date = as.Date("2017-01-01") + 0:9,
  value = rnorm(10)
)
as_tsibble(tbl1)
# supply the index to suppress the message
as_tsibble(tbl1, index = date)

# coerce tibble to tsibble with a single variable for key
# "date" is automatically considered as the index var, and "group" is the key
tbl2 <- tibble(
  mth = rep(yearmonth("2017-01") + 0:9, 3),
  group = rep(c("x", "y", "z"), each = 10),
  value = rnorm(30)
)
as_tsibble(tbl2, key = group)
as_tsibble(tbl2, key = group, index = mth)

# create a tsibble with multiple variables for key
tbl3 <- tibble(
  mth = rep(yearmonth("2010 Jan") + 0:8, each = 3),
  xyz = rep(c("x", "y", "z"), each = 9),
  abc = rep(letters[1:3], times = 9),
  value = rnorm(27)
)
as_tsibble(tbl3, key = c(xyz, abc))
# 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, pivot_longer = FALSE)
# }

Run the code above in your browser using DataLab