Functions to reshape multiple time series from 'wide' to 'long' and vice
versa. Note that long format data frames are ts-boxable objects, where wide
format data frames are not. ts_long
automatically identifies a time
column, and uses columns on the left as id columns.
Value
a ts-boxable object of the same class as x
, i.e., an object of
class ts
, xts
, zoo
, zooreg
, data.frame
, data.table
, tbl
,
tbl_ts
, tbl_time
, tis
, irts
or timeSeries
.
Examples
x <- ts_df(ts_c(mdeaths, fdeaths))
df.wide <- ts_wide(x)
df.wide
#> time mdeaths fdeaths
#> 1 1974-01-01 2134 901
#> 2 1974-02-01 1863 689
#> 3 1974-03-01 1877 827
#> 4 1974-04-01 1877 677
#> 5 1974-05-01 1492 522
#> 6 1974-06-01 1249 406
#> 7 1974-07-01 1280 441
#> 8 1974-08-01 1131 393
#> 9 1974-09-01 1209 387
#> 10 1974-10-01 1492 582
#> 11 1974-11-01 1621 578
#> 12 1974-12-01 1846 666
#> 13 1975-01-01 2103 830
#> 14 1975-02-01 2137 752
#> 15 1975-03-01 2153 785
#> 16 1975-04-01 1833 664
#> 17 1975-05-01 1403 467
#> 18 1975-06-01 1288 438
#> 19 1975-07-01 1186 421
#> 20 1975-08-01 1133 412
#> 21 1975-09-01 1053 343
#> 22 1975-10-01 1347 440
#> 23 1975-11-01 1545 531
#> 24 1975-12-01 2066 771
#> 25 1976-01-01 2020 767
#> 26 1976-02-01 2750 1141
#> 27 1976-03-01 2283 896
#> 28 1976-04-01 1479 532
#> 29 1976-05-01 1189 447
#> 30 1976-06-01 1160 420
#> 31 1976-07-01 1113 376
#> 32 1976-08-01 970 330
#> 33 1976-09-01 999 357
#> 34 1976-10-01 1208 445
#> 35 1976-11-01 1467 546
#> 36 1976-12-01 2059 764
#> 37 1977-01-01 2240 862
#> 38 1977-02-01 1634 660
#> 39 1977-03-01 1722 663
#> 40 1977-04-01 1801 643
#> 41 1977-05-01 1246 502
#> 42 1977-06-01 1162 392
#> 43 1977-07-01 1087 411
#> 44 1977-08-01 1013 348
#> 45 1977-09-01 959 387
#> 46 1977-10-01 1179 385
#> 47 1977-11-01 1229 411
#> 48 1977-12-01 1655 638
#> 49 1978-01-01 2019 796
#> 50 1978-02-01 2284 853
#> 51 1978-03-01 1942 737
#> 52 1978-04-01 1423 546
#> 53 1978-05-01 1340 530
#> 54 1978-06-01 1187 446
#> 55 1978-07-01 1098 431
#> 56 1978-08-01 1004 362
#> 57 1978-09-01 970 387
#> 58 1978-10-01 1140 430
#> 59 1978-11-01 1110 425
#> 60 1978-12-01 1812 679
#> 61 1979-01-01 2263 821
#> 62 1979-02-01 1820 785
#> 63 1979-03-01 1846 727
#> 64 1979-04-01 1531 612
#> 65 1979-05-01 1215 478
#> 66 1979-06-01 1075 429
#> 67 1979-07-01 1056 405
#> 68 1979-08-01 975 379
#> 69 1979-09-01 940 393
#> 70 1979-10-01 1081 411
#> 71 1979-11-01 1294 487
#> 72 1979-12-01 1341 574
ts_long(df.wide)
#> 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
#> 7 mdeaths 1974-07-01 1280
#> 8 mdeaths 1974-08-01 1131
#> 9 mdeaths 1974-09-01 1209
#> 10 mdeaths 1974-10-01 1492
#> 11 mdeaths 1974-11-01 1621
#> 12 mdeaths 1974-12-01 1846
#> 13 mdeaths 1975-01-01 2103
#> 14 mdeaths 1975-02-01 2137
#> 15 mdeaths 1975-03-01 2153
#> 16 mdeaths 1975-04-01 1833
#> 17 mdeaths 1975-05-01 1403
#> 18 mdeaths 1975-06-01 1288
#> 19 mdeaths 1975-07-01 1186
#> 20 mdeaths 1975-08-01 1133
#> 21 mdeaths 1975-09-01 1053
#> 22 mdeaths 1975-10-01 1347
#> 23 mdeaths 1975-11-01 1545
#> 24 mdeaths 1975-12-01 2066
#> 25 mdeaths 1976-01-01 2020
#> 26 mdeaths 1976-02-01 2750
#> 27 mdeaths 1976-03-01 2283
#> 28 mdeaths 1976-04-01 1479
#> 29 mdeaths 1976-05-01 1189
#> 30 mdeaths 1976-06-01 1160
#> 31 mdeaths 1976-07-01 1113
#> 32 mdeaths 1976-08-01 970
#> 33 mdeaths 1976-09-01 999
#> 34 mdeaths 1976-10-01 1208
#> 35 mdeaths 1976-11-01 1467
#> 36 mdeaths 1976-12-01 2059
#> 37 mdeaths 1977-01-01 2240
#> 38 mdeaths 1977-02-01 1634
#> 39 mdeaths 1977-03-01 1722
#> 40 mdeaths 1977-04-01 1801
#> 41 mdeaths 1977-05-01 1246
#> 42 mdeaths 1977-06-01 1162
#> 43 mdeaths 1977-07-01 1087
#> 44 mdeaths 1977-08-01 1013
#> 45 mdeaths 1977-09-01 959
#> 46 mdeaths 1977-10-01 1179
#> 47 mdeaths 1977-11-01 1229
#> 48 mdeaths 1977-12-01 1655
#> 49 mdeaths 1978-01-01 2019
#> 50 mdeaths 1978-02-01 2284
#> 51 mdeaths 1978-03-01 1942
#> 52 mdeaths 1978-04-01 1423
#> 53 mdeaths 1978-05-01 1340
#> 54 mdeaths 1978-06-01 1187
#> 55 mdeaths 1978-07-01 1098
#> 56 mdeaths 1978-08-01 1004
#> 57 mdeaths 1978-09-01 970
#> 58 mdeaths 1978-10-01 1140
#> 59 mdeaths 1978-11-01 1110
#> 60 mdeaths 1978-12-01 1812
#> 61 mdeaths 1979-01-01 2263
#> 62 mdeaths 1979-02-01 1820
#> 63 mdeaths 1979-03-01 1846
#> 64 mdeaths 1979-04-01 1531
#> 65 mdeaths 1979-05-01 1215
#> 66 mdeaths 1979-06-01 1075
#> 67 mdeaths 1979-07-01 1056
#> 68 mdeaths 1979-08-01 975
#> 69 mdeaths 1979-09-01 940
#> 70 mdeaths 1979-10-01 1081
#> 71 mdeaths 1979-11-01 1294
#> 72 mdeaths 1979-12-01 1341
#> 73 fdeaths 1974-01-01 901
#> 74 fdeaths 1974-02-01 689
#> 75 fdeaths 1974-03-01 827
#> 76 fdeaths 1974-04-01 677
#> 77 fdeaths 1974-05-01 522
#> 78 fdeaths 1974-06-01 406
#> 79 fdeaths 1974-07-01 441
#> 80 fdeaths 1974-08-01 393
#> 81 fdeaths 1974-09-01 387
#> 82 fdeaths 1974-10-01 582
#> 83 fdeaths 1974-11-01 578
#> 84 fdeaths 1974-12-01 666
#> 85 fdeaths 1975-01-01 830
#> 86 fdeaths 1975-02-01 752
#> 87 fdeaths 1975-03-01 785
#> 88 fdeaths 1975-04-01 664
#> 89 fdeaths 1975-05-01 467
#> 90 fdeaths 1975-06-01 438
#> 91 fdeaths 1975-07-01 421
#> 92 fdeaths 1975-08-01 412
#> 93 fdeaths 1975-09-01 343
#> 94 fdeaths 1975-10-01 440
#> 95 fdeaths 1975-11-01 531
#> 96 fdeaths 1975-12-01 771
#> 97 fdeaths 1976-01-01 767
#> 98 fdeaths 1976-02-01 1141
#> 99 fdeaths 1976-03-01 896
#> 100 fdeaths 1976-04-01 532
#> 101 fdeaths 1976-05-01 447
#> 102 fdeaths 1976-06-01 420
#> 103 fdeaths 1976-07-01 376
#> 104 fdeaths 1976-08-01 330
#> 105 fdeaths 1976-09-01 357
#> 106 fdeaths 1976-10-01 445
#> 107 fdeaths 1976-11-01 546
#> 108 fdeaths 1976-12-01 764
#> 109 fdeaths 1977-01-01 862
#> 110 fdeaths 1977-02-01 660
#> 111 fdeaths 1977-03-01 663
#> 112 fdeaths 1977-04-01 643
#> 113 fdeaths 1977-05-01 502
#> 114 fdeaths 1977-06-01 392
#> 115 fdeaths 1977-07-01 411
#> 116 fdeaths 1977-08-01 348
#> 117 fdeaths 1977-09-01 387
#> 118 fdeaths 1977-10-01 385
#> 119 fdeaths 1977-11-01 411
#> 120 fdeaths 1977-12-01 638
#> 121 fdeaths 1978-01-01 796
#> 122 fdeaths 1978-02-01 853
#> 123 fdeaths 1978-03-01 737
#> 124 fdeaths 1978-04-01 546
#> 125 fdeaths 1978-05-01 530
#> 126 fdeaths 1978-06-01 446
#> 127 fdeaths 1978-07-01 431
#> 128 fdeaths 1978-08-01 362
#> 129 fdeaths 1978-09-01 387
#> 130 fdeaths 1978-10-01 430
#> 131 fdeaths 1978-11-01 425
#> 132 fdeaths 1978-12-01 679
#> 133 fdeaths 1979-01-01 821
#> 134 fdeaths 1979-02-01 785
#> 135 fdeaths 1979-03-01 727
#> 136 fdeaths 1979-04-01 612
#> 137 fdeaths 1979-05-01 478
#> 138 fdeaths 1979-06-01 429
#> 139 fdeaths 1979-07-01 405
#> 140 fdeaths 1979-08-01 379
#> 141 fdeaths 1979-09-01 393
#> 142 fdeaths 1979-10-01 411
#> 143 fdeaths 1979-11-01 487
#> 144 fdeaths 1979-12-01 574