Skip to contents

Remove NA values in ts-boxable objects, turning explicit into implicit missing values.

Usage

ts_na_omit(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.

Details

Note that internal NAs in ts time series will not be removed, as this conflicts with the regular structure.

See also

ts_regular, for the opposite, turning implicit into explicit missing values.

Examples

x <- AirPassengers
x[c(2, 4)] <- NA

# A ts object does only know explicit NAs
ts_na_omit(x)
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 1949 112  NA 132  NA 121 135 148 148 136 119 104 118
#> 1950 115 126 141 135 125 149 170 170 158 133 114 140
#> 1951 145 150 178 163 172 178 199 199 184 162 146 166
#> 1952 171 180 193 181 183 218 230 242 209 191 172 194
#> 1953 196 196 236 235 229 243 264 272 237 211 180 201
#> 1954 204 188 235 227 234 264 302 293 259 229 203 229
#> 1955 242 233 267 269 270 315 364 347 312 274 237 278
#> 1956 284 277 317 313 318 374 413 405 355 306 271 306
#> 1957 315 301 356 348 355 422 465 467 404 347 305 336
#> 1958 340 318 362 348 363 435 491 505 404 359 310 337
#> 1959 360 342 406 396 420 472 548 559 463 407 362 405
#> 1960 417 391 419 461 472 535 622 606 508 461 390 432

# by default, NAs are implicit in data frames
ts_df(x)
#>           time value
#> 1   1949-01-01   112
#> 2   1949-02-01    NA
#> 3   1949-03-01   132
#> 4   1949-04-01    NA
#> 5   1949-05-01   121
#> 6   1949-06-01   135
#> 7   1949-07-01   148
#> 8   1949-08-01   148
#> 9   1949-09-01   136
#> 10  1949-10-01   119
#> 11  1949-11-01   104
#> 12  1949-12-01   118
#> 13  1950-01-01   115
#> 14  1950-02-01   126
#> 15  1950-03-01   141
#> 16  1950-04-01   135
#> 17  1950-05-01   125
#> 18  1950-06-01   149
#> 19  1950-07-01   170
#> 20  1950-08-01   170
#> 21  1950-09-01   158
#> 22  1950-10-01   133
#> 23  1950-11-01   114
#> 24  1950-12-01   140
#> 25  1951-01-01   145
#> 26  1951-02-01   150
#> 27  1951-03-01   178
#> 28  1951-04-01   163
#> 29  1951-05-01   172
#> 30  1951-06-01   178
#> 31  1951-07-01   199
#> 32  1951-08-01   199
#> 33  1951-09-01   184
#> 34  1951-10-01   162
#> 35  1951-11-01   146
#> 36  1951-12-01   166
#> 37  1952-01-01   171
#> 38  1952-02-01   180
#> 39  1952-03-01   193
#> 40  1952-04-01   181
#> 41  1952-05-01   183
#> 42  1952-06-01   218
#> 43  1952-07-01   230
#> 44  1952-08-01   242
#> 45  1952-09-01   209
#> 46  1952-10-01   191
#> 47  1952-11-01   172
#> 48  1952-12-01   194
#> 49  1953-01-01   196
#> 50  1953-02-01   196
#> 51  1953-03-01   236
#> 52  1953-04-01   235
#> 53  1953-05-01   229
#> 54  1953-06-01   243
#> 55  1953-07-01   264
#> 56  1953-08-01   272
#> 57  1953-09-01   237
#> 58  1953-10-01   211
#> 59  1953-11-01   180
#> 60  1953-12-01   201
#> 61  1954-01-01   204
#> 62  1954-02-01   188
#> 63  1954-03-01   235
#> 64  1954-04-01   227
#> 65  1954-05-01   234
#> 66  1954-06-01   264
#> 67  1954-07-01   302
#> 68  1954-08-01   293
#> 69  1954-09-01   259
#> 70  1954-10-01   229
#> 71  1954-11-01   203
#> 72  1954-12-01   229
#> 73  1955-01-01   242
#> 74  1955-02-01   233
#> 75  1955-03-01   267
#> 76  1955-04-01   269
#> 77  1955-05-01   270
#> 78  1955-06-01   315
#> 79  1955-07-01   364
#> 80  1955-08-01   347
#> 81  1955-09-01   312
#> 82  1955-10-01   274
#> 83  1955-11-01   237
#> 84  1955-12-01   278
#> 85  1956-01-01   284
#> 86  1956-02-01   277
#> 87  1956-03-01   317
#> 88  1956-04-01   313
#> 89  1956-05-01   318
#> 90  1956-06-01   374
#> 91  1956-07-01   413
#> 92  1956-08-01   405
#> 93  1956-09-01   355
#> 94  1956-10-01   306
#> 95  1956-11-01   271
#> 96  1956-12-01   306
#> 97  1957-01-01   315
#> 98  1957-02-01   301
#> 99  1957-03-01   356
#> 100 1957-04-01   348
#> 101 1957-05-01   355
#> 102 1957-06-01   422
#> 103 1957-07-01   465
#> 104 1957-08-01   467
#> 105 1957-09-01   404
#> 106 1957-10-01   347
#> 107 1957-11-01   305
#> 108 1957-12-01   336
#> 109 1958-01-01   340
#> 110 1958-02-01   318
#> 111 1958-03-01   362
#> 112 1958-04-01   348
#> 113 1958-05-01   363
#> 114 1958-06-01   435
#> 115 1958-07-01   491
#> 116 1958-08-01   505
#> 117 1958-09-01   404
#> 118 1958-10-01   359
#> 119 1958-11-01   310
#> 120 1958-12-01   337
#> 121 1959-01-01   360
#> 122 1959-02-01   342
#> 123 1959-03-01   406
#> 124 1959-04-01   396
#> 125 1959-05-01   420
#> 126 1959-06-01   472
#> 127 1959-07-01   548
#> 128 1959-08-01   559
#> 129 1959-09-01   463
#> 130 1959-10-01   407
#> 131 1959-11-01   362
#> 132 1959-12-01   405
#> 133 1960-01-01   417
#> 134 1960-02-01   391
#> 135 1960-03-01   419
#> 136 1960-04-01   461
#> 137 1960-05-01   472
#> 138 1960-06-01   535
#> 139 1960-07-01   622
#> 140 1960-08-01   606
#> 141 1960-09-01   508
#> 142 1960-10-01   461
#> 143 1960-11-01   390
#> 144 1960-12-01   432

# make NAs explicit
ts_regular(ts_df(x))
#>           time value
#> 1   1949-01-01   112
#> 2   1949-02-01    NA
#> 3   1949-03-01   132
#> 4   1949-04-01    NA
#> 5   1949-05-01   121
#> 6   1949-06-01   135
#> 7   1949-07-01   148
#> 8   1949-08-01   148
#> 9   1949-09-01   136
#> 10  1949-10-01   119
#> 11  1949-11-01   104
#> 12  1949-12-01   118
#> 13  1950-01-01   115
#> 14  1950-02-01   126
#> 15  1950-03-01   141
#> 16  1950-04-01   135
#> 17  1950-05-01   125
#> 18  1950-06-01   149
#> 19  1950-07-01   170
#> 20  1950-08-01   170
#> 21  1950-09-01   158
#> 22  1950-10-01   133
#> 23  1950-11-01   114
#> 24  1950-12-01   140
#> 25  1951-01-01   145
#> 26  1951-02-01   150
#> 27  1951-03-01   178
#> 28  1951-04-01   163
#> 29  1951-05-01   172
#> 30  1951-06-01   178
#> 31  1951-07-01   199
#> 32  1951-08-01   199
#> 33  1951-09-01   184
#> 34  1951-10-01   162
#> 35  1951-11-01   146
#> 36  1951-12-01   166
#> 37  1952-01-01   171
#> 38  1952-02-01   180
#> 39  1952-03-01   193
#> 40  1952-04-01   181
#> 41  1952-05-01   183
#> 42  1952-06-01   218
#> 43  1952-07-01   230
#> 44  1952-08-01   242
#> 45  1952-09-01   209
#> 46  1952-10-01   191
#> 47  1952-11-01   172
#> 48  1952-12-01   194
#> 49  1953-01-01   196
#> 50  1953-02-01   196
#> 51  1953-03-01   236
#> 52  1953-04-01   235
#> 53  1953-05-01   229
#> 54  1953-06-01   243
#> 55  1953-07-01   264
#> 56  1953-08-01   272
#> 57  1953-09-01   237
#> 58  1953-10-01   211
#> 59  1953-11-01   180
#> 60  1953-12-01   201
#> 61  1954-01-01   204
#> 62  1954-02-01   188
#> 63  1954-03-01   235
#> 64  1954-04-01   227
#> 65  1954-05-01   234
#> 66  1954-06-01   264
#> 67  1954-07-01   302
#> 68  1954-08-01   293
#> 69  1954-09-01   259
#> 70  1954-10-01   229
#> 71  1954-11-01   203
#> 72  1954-12-01   229
#> 73  1955-01-01   242
#> 74  1955-02-01   233
#> 75  1955-03-01   267
#> 76  1955-04-01   269
#> 77  1955-05-01   270
#> 78  1955-06-01   315
#> 79  1955-07-01   364
#> 80  1955-08-01   347
#> 81  1955-09-01   312
#> 82  1955-10-01   274
#> 83  1955-11-01   237
#> 84  1955-12-01   278
#> 85  1956-01-01   284
#> 86  1956-02-01   277
#> 87  1956-03-01   317
#> 88  1956-04-01   313
#> 89  1956-05-01   318
#> 90  1956-06-01   374
#> 91  1956-07-01   413
#> 92  1956-08-01   405
#> 93  1956-09-01   355
#> 94  1956-10-01   306
#> 95  1956-11-01   271
#> 96  1956-12-01   306
#> 97  1957-01-01   315
#> 98  1957-02-01   301
#> 99  1957-03-01   356
#> 100 1957-04-01   348
#> 101 1957-05-01   355
#> 102 1957-06-01   422
#> 103 1957-07-01   465
#> 104 1957-08-01   467
#> 105 1957-09-01   404
#> 106 1957-10-01   347
#> 107 1957-11-01   305
#> 108 1957-12-01   336
#> 109 1958-01-01   340
#> 110 1958-02-01   318
#> 111 1958-03-01   362
#> 112 1958-04-01   348
#> 113 1958-05-01   363
#> 114 1958-06-01   435
#> 115 1958-07-01   491
#> 116 1958-08-01   505
#> 117 1958-09-01   404
#> 118 1958-10-01   359
#> 119 1958-11-01   310
#> 120 1958-12-01   337
#> 121 1959-01-01   360
#> 122 1959-02-01   342
#> 123 1959-03-01   406
#> 124 1959-04-01   396
#> 125 1959-05-01   420
#> 126 1959-06-01   472
#> 127 1959-07-01   548
#> 128 1959-08-01   559
#> 129 1959-09-01   463
#> 130 1959-10-01   407
#> 131 1959-11-01   362
#> 132 1959-12-01   405
#> 133 1960-01-01   417
#> 134 1960-02-01   391
#> 135 1960-03-01   419
#> 136 1960-04-01   461
#> 137 1960-05-01   472
#> 138 1960-06-01   535
#> 139 1960-07-01   622
#> 140 1960-08-01   606
#> 141 1960-09-01   508
#> 142 1960-10-01   461
#> 143 1960-11-01   390
#> 144 1960-12-01   432

# and implicit again
ts_na_omit(ts_regular(ts_df(x)))
#>           time value
#> 1   1949-01-01   112
#> 2   1949-03-01   132
#> 3   1949-05-01   121
#> 4   1949-06-01   135
#> 5   1949-07-01   148
#> 6   1949-08-01   148
#> 7   1949-09-01   136
#> 8   1949-10-01   119
#> 9   1949-11-01   104
#> 10  1949-12-01   118
#> 11  1950-01-01   115
#> 12  1950-02-01   126
#> 13  1950-03-01   141
#> 14  1950-04-01   135
#> 15  1950-05-01   125
#> 16  1950-06-01   149
#> 17  1950-07-01   170
#> 18  1950-08-01   170
#> 19  1950-09-01   158
#> 20  1950-10-01   133
#> 21  1950-11-01   114
#> 22  1950-12-01   140
#> 23  1951-01-01   145
#> 24  1951-02-01   150
#> 25  1951-03-01   178
#> 26  1951-04-01   163
#> 27  1951-05-01   172
#> 28  1951-06-01   178
#> 29  1951-07-01   199
#> 30  1951-08-01   199
#> 31  1951-09-01   184
#> 32  1951-10-01   162
#> 33  1951-11-01   146
#> 34  1951-12-01   166
#> 35  1952-01-01   171
#> 36  1952-02-01   180
#> 37  1952-03-01   193
#> 38  1952-04-01   181
#> 39  1952-05-01   183
#> 40  1952-06-01   218
#> 41  1952-07-01   230
#> 42  1952-08-01   242
#> 43  1952-09-01   209
#> 44  1952-10-01   191
#> 45  1952-11-01   172
#> 46  1952-12-01   194
#> 47  1953-01-01   196
#> 48  1953-02-01   196
#> 49  1953-03-01   236
#> 50  1953-04-01   235
#> 51  1953-05-01   229
#> 52  1953-06-01   243
#> 53  1953-07-01   264
#> 54  1953-08-01   272
#> 55  1953-09-01   237
#> 56  1953-10-01   211
#> 57  1953-11-01   180
#> 58  1953-12-01   201
#> 59  1954-01-01   204
#> 60  1954-02-01   188
#> 61  1954-03-01   235
#> 62  1954-04-01   227
#> 63  1954-05-01   234
#> 64  1954-06-01   264
#> 65  1954-07-01   302
#> 66  1954-08-01   293
#> 67  1954-09-01   259
#> 68  1954-10-01   229
#> 69  1954-11-01   203
#> 70  1954-12-01   229
#> 71  1955-01-01   242
#> 72  1955-02-01   233
#> 73  1955-03-01   267
#> 74  1955-04-01   269
#> 75  1955-05-01   270
#> 76  1955-06-01   315
#> 77  1955-07-01   364
#> 78  1955-08-01   347
#> 79  1955-09-01   312
#> 80  1955-10-01   274
#> 81  1955-11-01   237
#> 82  1955-12-01   278
#> 83  1956-01-01   284
#> 84  1956-02-01   277
#> 85  1956-03-01   317
#> 86  1956-04-01   313
#> 87  1956-05-01   318
#> 88  1956-06-01   374
#> 89  1956-07-01   413
#> 90  1956-08-01   405
#> 91  1956-09-01   355
#> 92  1956-10-01   306
#> 93  1956-11-01   271
#> 94  1956-12-01   306
#> 95  1957-01-01   315
#> 96  1957-02-01   301
#> 97  1957-03-01   356
#> 98  1957-04-01   348
#> 99  1957-05-01   355
#> 100 1957-06-01   422
#> 101 1957-07-01   465
#> 102 1957-08-01   467
#> 103 1957-09-01   404
#> 104 1957-10-01   347
#> 105 1957-11-01   305
#> 106 1957-12-01   336
#> 107 1958-01-01   340
#> 108 1958-02-01   318
#> 109 1958-03-01   362
#> 110 1958-04-01   348
#> 111 1958-05-01   363
#> 112 1958-06-01   435
#> 113 1958-07-01   491
#> 114 1958-08-01   505
#> 115 1958-09-01   404
#> 116 1958-10-01   359
#> 117 1958-11-01   310
#> 118 1958-12-01   337
#> 119 1959-01-01   360
#> 120 1959-02-01   342
#> 121 1959-03-01   406
#> 122 1959-04-01   396
#> 123 1959-05-01   420
#> 124 1959-06-01   472
#> 125 1959-07-01   548
#> 126 1959-08-01   559
#> 127 1959-09-01   463
#> 128 1959-10-01   407
#> 129 1959-11-01   362
#> 130 1959-12-01   405
#> 131 1960-01-01   417
#> 132 1960-02-01   391
#> 133 1960-03-01   419
#> 134 1960-04-01   461
#> 135 1960-05-01   472
#> 136 1960-06-01   535
#> 137 1960-07-01   622
#> 138 1960-08-01   606
#> 139 1960-09-01   508
#> 140 1960-10-01   461
#> 141 1960-11-01   390
#> 142 1960-12-01   432