# Convert Everything to Everything

Source: `R/to_from_data.frame.R`, `R/to_from_data.table.R`, `R/to_from_tibble.R`, and 9 more
`ts_ts.Rd`

tsbox is built around a set of converters, which convert time series stored as `ts`, `xts`, `zoo`, `data.frame`, `data.table`, `tbl`, `tbl_ts`, `tbl_time`, `tis`, `irts` or `timeSeries` to each other.

    ts_data.frame(x)

    ts_df(x)

    ts_data.table(x)

    ts_dt(x)

    ts_tbl(x)

    ts_tibbletime(x)

    ts_timeSeries(x)

    ts_tis(x)

    ts_ts(x)

    ts_irts(x)

    ts_tsibble(x)

    ts_tslist(x)

    ts_xts(x)

    ts_zoo(x)

## Arguments

x | ts-boxable time series, an object of class `ts`, `xts`, `zoo`, `data.frame`, `data.table`, `tbl`, `tbl_ts`, `tbl_time`, `tis`, `irts` or `timeSeries`.

## Value

ts-boxable time series of the desired class, an object of class `ts`, `xts`, `zoo`, `data.frame`, `data.table`, `tbl`, `tbl_ts`, `tbl_time`, `tis`, `irts` or `timeSeries`.

## Details

In data frames, multiple time series will be stored in a 'long' format. tsbox detects a value, a time and zero to several id columns. Column detection is done in the following order:

1. Starting on the right, the first first `numeric` or `integer` column is used as value column.

2. Using the remaining columns, and starting on the right again, the first `Date`, `POSIXct`, `numeric` or `character` column is used as time column. `character` strings are parsed by `anytime::anytime()`. The time stamp, `time`, indicates the beginning of a period.

3. All remaining columns are id columns. Each unique combination of id columns points to a time series.

Alternatively, the time column and the value column to be explicitly named as `time` and `value`. If explicit names are used, the column order will be ignored.

Whenever possible, tsbox relies on heuristic time conversion. When a monthly `"ts"` time series, e.g., `AirPassengers`, is converted to a data frame, each time stamp (of class `"Date"`) is the first day of the month. In most circumstances, this reflects the actual meaning of the data stored in a `"ts"` object. Technically, of course, this is not correct: `"ts"` objects divide time in period of equal length, while in reality, February is shorter than January. Heuristic conversion is done for frequencies of 0.1 (decades), 1 (years), 4 (quarters) and 12 (month).

For other frequencies, e.g. 260, of `EuStockMarkets`, tsbox uses exact time conversion. The year is divided into 260 equally long units, and time stamp of a period will be a point in time (of class `"POSIXct"`).

## Examples

    x.ts <- ts_c(mdeaths, fdeaths)
    head(x.ts)
    #>          mdeaths fdeaths
    #> Jan 1974    2134     901
    #> Feb 1974    1863     689
    #> Mar 1974    1877     827
    #> Apr 1974    1877     677
    #> May 1974    1492     522
    #> Jun 1974    1249     406head(ts_df(x.ts))
    #>        id       time value
    #> 1 mdeaths 1974-01-01  2134
    #> 2 mdeaths 1974-02-01  1863
    #> 3 mdeaths 1974-03-01  1877
    #> 4 mdeaths 1974-04-01  1877
    #> 5 mdeaths 1974-05-01  1492
    #> 6 mdeaths 1974-06-01  1249
    suppressMessages(library(dplyr))
    head(ts_tbl(x.ts))
    #> # A tibble: 6 × 3
    #>   id      time       value
    #>   <chr>   <date>     <dbl>
    #> 1 mdeaths 1974-01-01  2134
    #> 2 mdeaths 1974-02-01  1863
    #> 3 mdeaths 1974-03-01  1877
    #> 4 mdeaths 1974-04-01  1877
    #> 5 mdeaths 1974-05-01  1492
    #> 6 mdeaths 1974-06-01  1249
    suppressMessages(library(data.table))
    head(ts_dt(x.ts))
    #>         id       time value
    #> 1: mdeaths 1974-01-01  2134
    #> 2: mdeaths 1974-02-01  1863
    #> 3: mdeaths 1974-03-01  1877
    #> 4: mdeaths 1974-04-01  1877
    #> 5: mdeaths 1974-05-01  1492
    #> 6: mdeaths 1974-06-01  1249
    suppressMessages(library(xts))
    head(ts_xts(x.ts))
    #>            mdeaths fdeaths
    #> 1974-01-01    2134     901
    #> 1974-02-01    1863     689
    #> 1974-03-01    1877     827
    #> 1974-04-01    1877     677
    #> 1974-05-01    1492     522
    #> 1974-06-01    1249     406
    # heuristic time conversion
    # 1 month: approx. 1/12 year
    head(ts_df(AirPassengers))
    #>         time value
    #> 1 1949-01-01   112
    #> 2 1949-02-01   118
    #> 3 1949-03-01   132
    #> 4 1949-04-01   129
    #> 5 1949-05-01   121
    #> 6 1949-06-01   135
    # exact time conversion
    # 1 trading day: exactly 1/260 year
    head(ts_df(EuStockMarkets))
    #>    id                time   value
    #> 1 DAX 1991-07-01 03:18:27 1628.75
    #> 2 DAX 1991-07-02 13:01:32 1613.63
    #> 3 DAX 1991-07-03 22:44:38 1606.51
    #> 4 DAX 1991-07-05 08:27:43 1621.04
    #> 5 DAX 1991-07-06 18:10:48 1618.16
    #> 6 DAX 1991-07-08 03:53:53 1610.61
    # multiple id
    multi.id.df <- rbind(
      within(ts_df(ts_c(fdeaths, mdeaths)), type <- "level"),
      within(ts_pc(ts_df(ts_c(fdeaths, mdeaths))), type <- "pc")
    )
    head(ts_ts(multi.id.df))
    #>          fdeaths_level mdeaths_level fdeaths_pc  mdeaths_pc
    #> Jan 1974           901          2134         NA          NA
    #> Feb 1974           689          1863  -23.52941 -12.6991565
    #> Mar 1974           827          1877   20.02903   0.7514761
    #> Apr 1974           677          1877  -18.13785   0.0000000
    #> May 1974           522          1492  -22.89513 -20.5114544
    #> Jun 1974           406          1249  -22.22222 -16.2868633ts_plot(multi.id.df)