@@ -180,76 +180,116 @@ test_that("Renaming columns while grouped gives appropriate colnames and metadat
180
180
181
181
test_that(" complete.epi_df works" , {
182
182
start_date <- as.Date(" 2020-01-01" )
183
- daily_edf <- tibble :: tibble(
184
- geo_value = c(1 , 2 ),
185
- time_value = start_date + c(1 , 2 ),
186
- value = 1
183
+ daily_edf <- tibble :: tribble(
184
+ ~ geo_value , ~ time_value , ~ value ,
185
+ 1 , start_date + 1 , 1 ,
186
+ 1 , start_date + 3 , 3 ,
187
+ 2 , start_date + 2 , 2 ,
188
+ 2 , start_date + 3 , 3 ,
187
189
) %> %
188
190
as_epi_df(as_of = start_date + 3 )
191
+ # Complete without grouping puts all the geo_values on the same min and max
192
+ # time_value index
189
193
expect_identical(
190
194
daily_edf %> %
191
- tidyr :: complete(geo_value , time_value = full_seq(time_value , period = 1 )),
192
- tibble :: tibble(
193
- geo_value = c(1 , 1 , 2 , 2 ),
194
- time_value = start_date + c(1 , 2 , 1 , 2 ),
195
- value = c(1 , NA , NA , 1 )
195
+ complete(geo_value , time_value = full_seq(time_value , period = 1 )),
196
+ tibble :: tribble(
197
+ ~ geo_value , ~ time_value , ~ value ,
198
+ 1 , start_date + 1 , 1 ,
199
+ 1 , start_date + 2 , NA ,
200
+ 1 , start_date + 3 , 3 ,
201
+ 2 , start_date + 1 , NA ,
202
+ 2 , start_date + 2 , 2 ,
203
+ 2 , start_date + 3 , 3 ,
196
204
) %> %
197
205
as_epi_df(as_of = start_date + 3 )
198
206
)
207
+ # Complete with grouping puts all the geo_values on individual min and max
208
+ # time_value indices
199
209
expect_identical(
200
210
daily_edf %> %
201
211
group_by(geo_value ) %> %
202
- tidyr :: complete(time_value = full_seq(time_value , period = 1 )),
203
- daily_edf %> %
212
+ complete(time_value = full_seq(time_value , period = 1 )),
213
+ tibble :: tribble(
214
+ ~ geo_value , ~ time_value , ~ value ,
215
+ 1 , start_date + 1 , 1 ,
216
+ 1 , start_date + 2 , NA ,
217
+ 1 , start_date + 3 , 3 ,
218
+ 2 , start_date + 2 , 2 ,
219
+ 2 , start_date + 3 , 3 ,
220
+ ) %> %
221
+ as_epi_df(as_of = start_date + 3 ) %> %
204
222
group_by(geo_value )
205
223
)
206
- weekly_edf <- tibble :: tibble(
207
- geo_value = c(1 , 1 , 1 , 2 ),
208
- time_value = start_date + c(1 , 15 , 22 , 1 ),
209
- value = 1
224
+ # Complete has explicit=TRUE by default, but if it's FALSE, then complete only fills the implicit gaps
225
+ # not those that are explicitly NA
226
+ daily_edf <- tibble :: tribble(
227
+ ~ geo_value , ~ time_value , ~ value ,
228
+ 1 , start_date + 1 , 1 ,
229
+ 1 , start_date + 2 , NA ,
230
+ 1 , start_date + 3 , 3 ,
231
+ 2 , start_date + 2 , 2 ,
232
+ 2 , start_date + 3 , 3 ,
210
233
) %> %
211
234
as_epi_df(as_of = start_date + 3 )
212
235
expect_identical(
213
- weekly_edf %> %
214
- complete(geo_value ,
215
- time_value = full_seq(time_value , period = 7 ),
216
- fill = list (value = 5 )
217
- ),
218
- tibble :: tibble(
219
- geo_value = c(1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ),
220
- time_value = start_date + c(1 , 8 , 15 , 22 , 1 , 8 , 15 , 22 ),
221
- value = c(1 , 5 , 1 , 1 , 1 , 5 , 5 , 5 )
236
+ daily_edf %> %
237
+ complete(geo_value , time_value = full_seq(time_value , period = 1 ), fill = list (value = 0 ), explicit = FALSE ),
238
+ tibble :: tribble(
239
+ ~ geo_value , ~ time_value , ~ value ,
240
+ 1 , start_date + 1 , 1 ,
241
+ 1 , start_date + 2 , NA ,
242
+ 1 , start_date + 3 , 3 ,
243
+ 2 , start_date + 1 , 0 ,
244
+ 2 , start_date + 2 , 2 ,
245
+ 2 , start_date + 3 , 3 ,
222
246
) %> %
223
247
as_epi_df(as_of = start_date + 3 )
224
248
)
249
+ # Complete works for weekly data and can take a fill value
250
+ # No grouping
251
+ weekly_edf <- tibble :: tribble(
252
+ ~ geo_value , ~ time_value , ~ value ,
253
+ 1 , start_date + 1 , 1 ,
254
+ 1 , start_date + 15 , 3 ,
255
+ 2 , start_date + 8 , 2 ,
256
+ 2 , start_date + 15 , 3 ,
257
+ ) %> %
258
+ as_epi_df(as_of = start_date + 3 )
225
259
expect_identical(
226
260
weekly_edf %> %
227
- group_by(geo_value ) %> %
228
- complete(
261
+ complete(geo_value ,
229
262
time_value = full_seq(time_value , period = 7 ),
230
- fill = list (value = 5 )
231
- ) %> %
232
- ungroup(),
233
- tibble :: tibble(
234
- geo_value = c(1 , 1 , 1 , 1 , 2 ),
235
- time_value = start_date + c(1 , 8 , 15 , 22 , 1 ),
236
- value = c(1 , 5 , 1 , 1 , 1 )
263
+ fill = list (value = 0 )
264
+ ),
265
+ tibble :: tribble(
266
+ ~ geo_value , ~ time_value , ~ value ,
267
+ 1 , start_date + 1 , 1 ,
268
+ 1 , start_date + 8 , 0 ,
269
+ 1 , start_date + 15 , 3 ,
270
+ 2 , start_date + 1 , 0 ,
271
+ 2 , start_date + 8 , 2 ,
272
+ 2 , start_date + 15 , 3 ,
237
273
) %> %
238
274
as_epi_df(as_of = start_date + 3 )
239
275
)
276
+ # With grouping
240
277
expect_identical(
241
278
weekly_edf %> %
242
279
group_by(geo_value ) %> %
243
280
complete(
244
- time_value = full_seq(time_value , period = 1 ),
245
- fill = list (value = 5 )
246
- ) %> %
247
- ungroup(),
248
- tibble :: tibble(
249
- geo_value = c(rep(1 , 22 ), 2 ),
250
- time_value = start_date + c(1 : 22 , 1 ),
251
- value = c(1 , rep(5 , 13 ), 1 , rep(5 , 6 ), 1 , 1 )
281
+ time_value = full_seq(time_value , period = 7 ),
282
+ fill = list (value = 0 )
283
+ ),
284
+ tibble :: tribble(
285
+ ~ geo_value , ~ time_value , ~ value ,
286
+ 1 , start_date + 1 , 1 ,
287
+ 1 , start_date + 8 , 0 ,
288
+ 1 , start_date + 15 , 3 ,
289
+ 2 , start_date + 8 , 2 ,
290
+ 2 , start_date + 15 , 3 ,
252
291
) %> %
253
- as_epi_df(as_of = start_date + 3 )
292
+ as_epi_df(as_of = start_date + 3 ) %> %
293
+ group_by(geo_value )
254
294
)
255
295
})
0 commit comments