Learn R Programming

tsibble (version 0.5.3)

count_gaps: Count implicit gaps

Description

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

Usage

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

Atomic vectors. 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

fill_na

Examples

Run this code
# NOT RUN {
# Implicit missing time without group_by ----
# All the sensors have 2 common missing time points in the data
count_gaps(pedestrian)
# Time gaps for each sensor per month ----
pedestrian %>% 
  index_by(yrmth = yearmonth(Date)) %>% 
  group_by(Sensor) %>% 
  count_gaps()
# 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)
# }

Run the code above in your browser using DataLab