Skip to contents

Replace date or time values by the first of the period. tsbox usually relies on timestamps being the first value of a period.

Usage

ts_first_of_period(x)

Arguments

x

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

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_c(
  a = ts_lag(ts_df(mdeaths), "14 days"),
  b = ts_lag(ts_df(mdeaths), "-2 days")
)
ts_first_of_period(x)
#>     id       time value
#> 1    a 1974-01-01  2134
#> 2    a 1974-02-01  1863
#> 3    a 1974-03-01  1877
#> 4    a 1974-04-01  1877
#> 5    a 1974-05-01  1492
#> 6    a 1974-06-01  1249
#> 7    a 1974-07-01  1280
#> 8    a 1974-08-01  1131
#> 9    a 1974-09-01  1209
#> 10   a 1974-10-01  1492
#> 11   a 1974-11-01  1621
#> 12   a 1974-12-01  1846
#> 13   a 1975-01-01  2103
#> 14   a 1975-02-01  2137
#> 15   a 1975-03-01  2153
#> 16   a 1975-04-01  1833
#> 17   a 1975-05-01  1403
#> 18   a 1975-06-01  1288
#> 19   a 1975-07-01  1186
#> 20   a 1975-08-01  1133
#> 21   a 1975-09-01  1053
#> 22   a 1975-10-01  1347
#> 23   a 1975-11-01  1545
#> 24   a 1975-12-01  2066
#> 25   a 1976-01-01  2020
#> 26   a 1976-02-01  2750
#> 27   a 1976-03-01  2283
#> 28   a 1976-04-01  1479
#> 29   a 1976-05-01  1189
#> 30   a 1976-06-01  1160
#> 31   a 1976-07-01  1113
#> 32   a 1976-08-01   970
#> 33   a 1976-09-01   999
#> 34   a 1976-10-01  1208
#> 35   a 1976-11-01  1467
#> 36   a 1976-12-01  2059
#> 37   a 1977-01-01  2240
#> 38   a 1977-02-01  1634
#> 39   a 1977-03-01  1722
#> 40   a 1977-04-01  1801
#> 41   a 1977-05-01  1246
#> 42   a 1977-06-01  1162
#> 43   a 1977-07-01  1087
#> 44   a 1977-08-01  1013
#> 45   a 1977-09-01   959
#> 46   a 1977-10-01  1179
#> 47   a 1977-11-01  1229
#> 48   a 1977-12-01  1655
#> 49   a 1978-01-01  2019
#> 50   a 1978-02-01  2284
#> 51   a 1978-03-01  1942
#> 52   a 1978-04-01  1423
#> 53   a 1978-05-01  1340
#> 54   a 1978-06-01  1187
#> 55   a 1978-07-01  1098
#> 56   a 1978-08-01  1004
#> 57   a 1978-09-01   970
#> 58   a 1978-10-01  1140
#> 59   a 1978-11-01  1110
#> 60   a 1978-12-01  1812
#> 61   a 1979-01-01  2263
#> 62   a 1979-02-01  1820
#> 63   a 1979-03-01  1846
#> 64   a 1979-04-01  1531
#> 65   a 1979-05-01  1215
#> 66   a 1979-06-01  1075
#> 67   a 1979-07-01  1056
#> 68   a 1979-08-01   975
#> 69   a 1979-09-01   940
#> 70   a 1979-10-01  1081
#> 71   a 1979-11-01  1294
#> 72   a 1979-12-01  1341
#> 73   b 1973-12-01  2134
#> 74   b 1974-01-01  1863
#> 75   b 1974-02-01  1877
#> 76   b 1974-03-01  1877
#> 77   b 1974-04-01  1492
#> 78   b 1974-05-01  1249
#> 79   b 1974-06-01  1280
#> 80   b 1974-07-01  1131
#> 81   b 1974-08-01  1209
#> 82   b 1974-09-01  1492
#> 83   b 1974-10-01  1621
#> 84   b 1974-11-01  1846
#> 85   b 1974-12-01  2103
#> 86   b 1975-01-01  2137
#> 87   b 1975-02-01  2153
#> 88   b 1975-03-01  1833
#> 89   b 1975-04-01  1403
#> 90   b 1975-05-01  1288
#> 91   b 1975-06-01  1186
#> 92   b 1975-07-01  1133
#> 93   b 1975-08-01  1053
#> 94   b 1975-09-01  1347
#> 95   b 1975-10-01  1545
#> 96   b 1975-11-01  2066
#> 97   b 1975-12-01  2020
#> 98   b 1976-01-01  2750
#> 99   b 1976-02-01  2283
#> 100  b 1976-03-01  1479
#> 101  b 1976-04-01  1189
#> 102  b 1976-05-01  1160
#> 103  b 1976-06-01  1113
#> 104  b 1976-07-01   970
#> 105  b 1976-08-01   999
#> 106  b 1976-09-01  1208
#> 107  b 1976-10-01  1467
#> 108  b 1976-11-01  2059
#> 109  b 1976-12-01  2240
#> 110  b 1977-01-01  1634
#> 111  b 1977-02-01  1722
#> 112  b 1977-03-01  1801
#> 113  b 1977-04-01  1246
#> 114  b 1977-05-01  1162
#> 115  b 1977-06-01  1087
#> 116  b 1977-07-01  1013
#> 117  b 1977-08-01   959
#> 118  b 1977-09-01  1179
#> 119  b 1977-10-01  1229
#> 120  b 1977-11-01  1655
#> 121  b 1977-12-01  2019
#> 122  b 1978-01-01  2284
#> 123  b 1978-02-01  1942
#> 124  b 1978-03-01  1423
#> 125  b 1978-04-01  1340
#> 126  b 1978-05-01  1187
#> 127  b 1978-06-01  1098
#> 128  b 1978-07-01  1004
#> 129  b 1978-08-01   970
#> 130  b 1978-09-01  1140
#> 131  b 1978-10-01  1110
#> 132  b 1978-11-01  1812
#> 133  b 1978-12-01  2263
#> 134  b 1979-01-01  1820
#> 135  b 1979-02-01  1846
#> 136  b 1979-03-01  1531
#> 137  b 1979-04-01  1215
#> 138  b 1979-05-01  1075
#> 139  b 1979-06-01  1056
#> 140  b 1979-07-01   975
#> 141  b 1979-08-01   940
#> 142  b 1979-09-01  1081
#> 143  b 1979-10-01  1294
#> 144  b 1979-11-01  1341
ts_first_of_period(ts_lag(ts_df(austres), "14 days"))
#>          time   value
#> 1  1971-04-01 13067.3
#> 2  1971-07-01 13130.5
#> 3  1971-10-01 13198.4
#> 4  1972-01-01 13254.2
#> 5  1972-04-01 13303.7
#> 6  1972-07-01 13353.9
#> 7  1972-10-01 13409.3
#> 8  1973-01-01 13459.2
#> 9  1973-04-01 13504.5
#> 10 1973-07-01 13552.6
#> 11 1973-10-01 13614.3
#> 12 1974-01-01 13669.5
#> 13 1974-04-01 13722.6
#> 14 1974-07-01 13772.1
#> 15 1974-10-01 13832.0
#> 16 1975-01-01 13862.6
#> 17 1975-04-01 13893.0
#> 18 1975-07-01 13926.8
#> 19 1975-10-01 13968.9
#> 20 1976-01-01 14004.7
#> 21 1976-04-01 14033.1
#> 22 1976-07-01 14066.0
#> 23 1976-10-01 14110.1
#> 24 1977-01-01 14155.6
#> 25 1977-04-01 14192.2
#> 26 1977-07-01 14231.7
#> 27 1977-10-01 14281.5
#> 28 1978-01-01 14330.3
#> 29 1978-04-01 14359.3
#> 30 1978-07-01 14396.6
#> 31 1978-10-01 14430.8
#> 32 1979-01-01 14478.4
#> 33 1979-04-01 14515.7
#> 34 1979-07-01 14554.9
#> 35 1979-10-01 14602.5
#> 36 1980-01-01 14646.4
#> 37 1980-04-01 14695.4
#> 38 1980-07-01 14746.6
#> 39 1980-10-01 14807.4
#> 40 1981-01-01 14874.4
#> 41 1981-04-01 14923.3
#> 42 1981-07-01 14988.7
#> 43 1981-10-01 15054.1
#> 44 1982-01-01 15121.7
#> 45 1982-04-01 15184.2
#> 46 1982-07-01 15239.3
#> 47 1982-10-01 15288.9
#> 48 1983-01-01 15346.2
#> 49 1983-04-01 15393.5
#> 50 1983-07-01 15439.0
#> 51 1983-10-01 15483.5
#> 52 1984-01-01 15531.5
#> 53 1984-04-01 15579.4
#> 54 1984-07-01 15628.5
#> 55 1984-10-01 15677.3
#> 56 1985-01-01 15736.7
#> 57 1985-04-01 15788.3
#> 58 1985-07-01 15839.7
#> 59 1985-10-01 15900.6
#> 60 1986-01-01 15961.5
#> 61 1986-04-01 16018.3
#> 62 1986-07-01 16076.9
#> 63 1986-10-01 16139.0
#> 64 1987-01-01 16203.0
#> 65 1987-04-01 16263.3
#> 66 1987-07-01 16327.9
#> 67 1987-10-01 16398.9
#> 68 1988-01-01 16478.3
#> 69 1988-04-01 16538.2
#> 70 1988-07-01 16621.6
#> 71 1988-10-01 16697.0
#> 72 1989-01-01 16777.2
#> 73 1989-04-01 16833.1
#> 74 1989-07-01 16891.6
#> 75 1989-10-01 16956.8
#> 76 1990-01-01 17026.3
#> 77 1990-04-01 17085.4
#> 78 1990-07-01 17106.9
#> 79 1990-10-01 17169.4
#> 80 1991-01-01 17239.4
#> 81 1991-04-01 17292.0
#> 82 1991-07-01 17354.2
#> 83 1991-10-01 17414.2
#> 84 1992-01-01 17447.3
#> 85 1992-04-01 17482.6
#> 86 1992-07-01 17526.0
#> 87 1992-10-01 17568.7
#> 88 1993-01-01 17627.1
#> 89 1993-04-01 17661.5