Coerce to a tsibble object

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, ...)

# 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, ...)

Arguments

x

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

...

Other arguments passed on to individual methods.

key

Variable(s) that define unique time indices, used in conjunction with the helper id(). If a univariate time series (without an explicit key), simply call id().

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.

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

Examples

# coerce tibble to tsibble w/o a key ---- tbl1 <- tibble( date = as.Date("2017-01-01") + 0:9, value = rnorm(10) ) as_tsibble(tbl1)
#> Column `date` is the index.
#> # A tsibble: 10 x 2 [1D] #> date value #> <date> <dbl> #> 1 2017-01-01 -1.40 #> 2 2017-01-02 0.255 #> 3 2017-01-03 -2.44 #> 4 2017-01-04 -0.00557 #> 5 2017-01-05 0.622 #> 6 2017-01-06 1.15 #> 7 2017-01-07 -1.82 #> 8 2017-01-08 -0.247 #> 9 2017-01-09 -0.244 #> 10 2017-01-10 -0.283
# specify the index var as_tsibble(tbl1, index = date)
#> # A tsibble: 10 x 2 [1D] #> date value #> <date> <dbl> #> 1 2017-01-01 -1.40 #> 2 2017-01-02 0.255 #> 3 2017-01-03 -2.44 #> 4 2017-01-04 -0.00557 #> 5 2017-01-05 0.622 #> 6 2017-01-06 1.15 #> 7 2017-01-07 -1.82 #> 8 2017-01-08 -0.247 #> 9 2017-01-09 -0.244 #> 10 2017-01-10 -0.283
# 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("2017-01") + 0:9, 3), group = rep(c("x", "y", "z"), each = 10), value = rnorm(30) ) as_tsibble(tbl2, key = id(group))
#> Column `mth` is the index.
#> # A tsibble: 30 x 3 [1M] #> # Key: group [3] #> mth group value #> <mth> <chr> <dbl> #> 1 2017 Jan x -0.554 #> 2 2017 Feb x 0.629 #> 3 2017 Mar x 2.07 #> 4 2017 Apr x -1.63 #> 5 2017 May x 0.512 #> 6 2017 Jun x -1.86 #> 7 2017 Jul x -0.522 #> 8 2017 Aug x -0.0526 #> 9 2017 Sep x 0.543 #> 10 2017 Oct x -0.914 #> # … with 20 more rows
as_tsibble(tbl2, key = id(group), index = mth)
#> # A tsibble: 30 x 3 [1M] #> # Key: group [3] #> mth group value #> <mth> <chr> <dbl> #> 1 2017 Jan x -0.554 #> 2 2017 Feb x 0.629 #> 3 2017 Mar x 2.07 #> 4 2017 Apr x -1.63 #> 5 2017 May x 0.512 #> 6 2017 Jun x -1.86 #> 7 2017 Jul x -0.522 #> 8 2017 Aug x -0.0526 #> 9 2017 Sep x 0.543 #> 10 2017 Oct x -0.914 #> # … with 20 more rows
# coerce ts to tsibble as_tsibble(AirPassengers)
#> # A tsibble: 144 x 2 [1M] #> index value #> <mth> <dbl> #> 1 1949 Jan 112 #> 2 1949 Feb 118 #> 3 1949 Mar 132 #> 4 1949 Apr 129 #> 5 1949 May 121 #> 6 1949 Jun 135 #> 7 1949 Jul 148 #> 8 1949 Aug 148 #> 9 1949 Sep 136 #> 10 1949 Oct 119 #> # … with 134 more rows
as_tsibble(sunspot.year)
#> # A tsibble: 289 x 2 [1Y] #> index value #> <dbl> <dbl> #> 1 1700 5 #> 2 1701 11 #> 3 1702 16 #> 4 1703 23 #> 5 1704 36 #> 6 1705 58 #> 7 1706 29 #> 8 1707 20 #> 9 1708 10 #> 10 1709 8 #> # … with 279 more rows
as_tsibble(sunspot.month)
#> # A tsibble: 3,177 x 2 [1M] #> index value #> <mth> <dbl> #> 1 1749 Jan 58 #> 2 1749 Feb 62.6 #> 3 1749 Mar 70 #> 4 1749 Apr 55.7 #> 5 1749 May 85 #> 6 1749 Jun 83.5 #> 7 1749 Jul 94.8 #> 8 1749 Aug 66.3 #> 9 1749 Sep 75.9 #> 10 1749 Oct 75.5 #> # … with 3,167 more rows
as_tsibble(austres)
#> # A tsibble: 89 x 2 [1Q] #> index value #> <qtr> <dbl> #> 1 1971 Q2 13067. #> 2 1971 Q3 13130. #> 3 1971 Q4 13198. #> 4 1972 Q1 13254. #> 5 1972 Q2 13304. #> 6 1972 Q3 13354. #> 7 1972 Q4 13409. #> 8 1973 Q1 13459. #> 9 1973 Q2 13504. #> 10 1973 Q3 13553. #> # … with 79 more rows
# coerce mts to tsibble z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12) as_tsibble(z)
#> # A tsibble: 300 x 3 [1M] #> # Key: key [3] #> index key value #> <mth> <chr> <dbl> #> 1 1961 Jan Series 1 0.0700 #> 2 1961 Feb Series 1 -0.639 #> 3 1961 Mar Series 1 -0.0500 #> 4 1961 Apr Series 1 -0.251 #> 5 1961 May Series 1 0.445 #> 6 1961 Jun Series 1 2.76 #> 7 1961 Jul Series 1 0.0465 #> 8 1961 Aug Series 1 0.578 #> 9 1961 Sep Series 1 0.118 #> 10 1961 Oct Series 1 -1.91 #> # … with 290 more rows
as_tsibble(z, gather = FALSE)
#> # A tsibble: 100 x 4 [1M] #> index `Series 1` `Series 2` `Series 3` #> <mth> <dbl> <dbl> <dbl> #> 1 1961 Jan 0.0700 -0.163 -0.903 #> 2 1961 Feb -0.639 -0.827 1.32 #> 3 1961 Mar -0.0500 1.88 1.10 #> 4 1961 Apr -0.251 0.766 1.20 #> 5 1961 May 0.445 0.980 -1.43 #> 6 1961 Jun 2.76 1.32 1.38 #> 7 1961 Jul 0.0465 -1.12 0.00313 #> 8 1961 Aug 0.578 0.515 -0.0779 #> 9 1961 Sep 0.118 -1.51 0.441 #> 10 1961 Oct -1.91 1.53 0.129 #> # … with 90 more rows