count_gaps() counts gaps for a tsibble; gaps() find where the gaps in x with respect to y.

count_gaps(.data, ...)

# S3 method for tbl_ts
count_gaps(.data, ...)

# S3 method for grouped_ts
count_gaps(.data, .full = FALSE, ...)

gaps(x, y)

Arguments

.data

A tbl_ts.

...

Other arguments passed on to individual methods.

.full

FALSE to find gaps for each group within its own period. TRUE to find gaps over the entire time span of the data.

x, y

A vector of numbers, dates, or date-times. The length of y must be greater than the length of x.

Value

A tibble contains:

  • the "key" of the tbl_ts

  • "from": the starting time point of the gap

  • "end": the ending time point of the gap

  • "n": the implicit missing observations during the time period

See also

Examples

# Implicit missing time without group_by ---- # All the sensors have 2 common missing time points in the data count_gaps(pedestrian)
#> # A tibble: 2 x 3 #> from to n #> <dttm> <dttm> <int> #> 1 2015-04-05 02:00:00 2015-04-05 02:00:00 1 #> 2 2016-04-03 02:00:00 2016-04-03 02:00:00 1
# Time gaps for each sensor per month ---- pedestrian %>% index_by(yrmth = yearmonth(Date)) %>% group_by(Sensor) %>% count_gaps()
#> # A tibble: 97 x 5 #> Sensor yrmth from to n #> <chr> <mth> <dttm> <dttm> <int> #> 1 Birrarung Marr 2015 Jan NA NA 0 #> 2 Birrarung Marr 2015 Feb NA NA 0 #> 3 Birrarung Marr 2015 Mar NA NA 0 #> 4 Birrarung Marr 2015 Apr 2015-04-05 02:00:00 2015-04-05 02:00:00 1 #> 5 Birrarung Marr 2015 May NA NA 0 #> 6 Birrarung Marr 2015 Jun NA NA 0 #> 7 Birrarung Marr 2015 Jul NA NA 0 #> 8 Birrarung Marr 2015 Aug NA NA 0 #> 9 Birrarung Marr 2015 Sep NA NA 0 #> 10 Birrarung Marr 2015 Oct NA NA 0 #> # ... with 87 more rows
# Time gaps for each sensor ---- ped_gaps <- pedestrian %>% group_by(Sensor) %>% count_gaps(.full = TRUE) if (!requireNamespace("ggplot2", quietly = TRUE)) { stop("Please install the ggplot2 package to run these following examples.") } library(ggplot2) ggplot(ped_gaps, aes(colour = Sensor)) + geom_linerange(aes(x = Sensor, ymin = from, ymax = to)) + geom_point(aes(x = Sensor, y = from)) + geom_point(aes(x = Sensor, y = to)) + coord_flip() + theme(legend.position = "bottom")
# Vectors ---- gaps(x = c(1:3, 5:6, 9:10), y = 1:10)
#> # A tibble: 2 x 3 #> from to n #> <int> <int> <int> #> 1 4 4 1 #> 2 7 8 2