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 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 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). TRUE finds the minimal time span as the interval.

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

# coerce tibble to tsibble w/o a key ---- tbl1 <- tibble::tibble( date = seq(as.Date("2017-01-01"), as.Date("2017-01-10"), by = 1), value = rnorm(10) ) as_tsibble(tbl1)
#> The 'index' variable: date
#> # A tsibble: 10 x 2 [1DAY] #> 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 [1DAY] #> 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::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))
#> The 'index' variable: mth
#> # A tsibble: 30 x 3 [1MONTH] #> # Keys: 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 [1MONTH] #> # Keys: 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 [1MONTH] #> 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 [1YEAR] #> index value #> <dbl> <dbl> #> 1 1700 5.00 #> 2 1701 11.0 #> 3 1702 16.0 #> 4 1703 23.0 #> 5 1704 36.0 #> 6 1705 58.0 #> 7 1706 29.0 #> 8 1707 20.0 #> 9 1708 10.0 #> 10 1709 8.00 #> # ... with 279 more rows
as_tsibble(sunspot.month)
#> # A tsibble: 3,177 x 2 [1MONTH] #> index value #> <mth> <dbl> #> 1 1749 Jan 58.0 #> 2 1749 Feb 62.6 #> 3 1749 Mar 70.0 #> 4 1749 Apr 55.7 #> 5 1749 May 85.0 #> 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 [1QUARTER] #> 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 [1MONTH] #> # Keys: 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 [1MONTH] #> 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
# 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)
#> # A tsibble: 50 x 4 [1YEAR] #> # Keys: `Level 2` | `Level 1` [5] #> index value `Level 2` `Level 1` #> <dbl> <dbl> <chr> <chr> #> 1 1992 7.86 AA A #> 2 1993 8.34 AA A #> 3 1994 8.55 AA A #> 4 1995 8.60 AA A #> 5 1996 8.77 AA A #> 6 1997 8.84 AA A #> 7 1998 8.89 AA A #> 8 1999 8.93 AA A #> 9 2000 9.13 AA A #> 10 2001 9.15 AA A #> # ... with 40 more rows
as_tsibble(hts::htseg2)
#> # A tsibble: 160 x 5 [1YEAR] #> # Keys: `Level 3` | `Level 2` | `Level 1` [10] #> index value `Level 3` `Level 2` `Level 1` #> <dbl> <dbl> <chr> <chr> <chr> #> 1 1992 -2.42 A10A A10 A #> 2 1993 -2.20 A10A A10 A #> 3 1994 -2.19 A10A A10 A #> 4 1995 -1.93 A10A A10 A #> 5 1996 -1.64 A10A A10 A #> 6 1997 -1.63 A10A A10 A #> 7 1998 -1.61 A10A A10 A #> 8 1999 -1.59 A10A A10 A #> 9 2000 -1.53 A10A A10 A #> 10 2001 -1.39 A10A A10 A #> # ... with 150 more rows