R: Turn implicit missing values into explicit missing values

fill_gaps {tsibble} | R Documentation

## Turn implicit missing values into explicit missing values

### Description

Turn implicit missing values into explicit missing values

### Usage

    fill_gaps(.data, ..., .full = FALSE)

### Arguments

`.data` | A tsibble.
`...` | A set of name-value pairs. The values provided will only replace missing values that were marked as "implicit", and will leave previously existing `NA` untouched. empty: filled with default `NA`. filled by values or functions.
`.full` | `FALSE` to insert `NA` for each series within its own period. `TRUE` to fill `NA` over the entire time span of the data (a.k.a. fully balanced panel).

### See Also

tidyr::fill, tidyr::replace_na for handling missing values `NA`.

Other implicit gaps handling: `count_gaps`, `has_gaps`, `scan_gaps`

### Examples

    harvest <- tsibble(
      year = c(2010, 2011, 2013, 2011, 2012, 2014),
      fruit = rep(c("kiwi", "cherry"), each = 3),
      kilo = sample(1:10, size = 6),
      key = id(fruit), index = year
    )

    # gaps as default `NA` ----
    fill_gaps(harvest, .full = TRUE)
    full_harvest <- fill_gaps(harvest, .full = FALSE)
    full_harvest

    # use fill() to fill `NA` by previous/next entry
    full_harvest %>%
      group_by(fruit) %>%
      fill(kilo, .direction = "down")

    # replace gaps with a specific value ----
    harvest %>%
      fill_gaps(kilo = 0L)

    # replace gaps using a function by variable ----
    harvest %>%
      fill_gaps(kilo = sum(kilo))

    # replace gaps using a function for each group ----
    harvest %>%
      group_by(fruit) %>%
      fill_gaps(kilo = sum(kilo))

    # leaves existing `NA` untouched ----
    harvest[2, 3] <- NA
    harvest %>%
      group_by(fruit) %>%
      fill_gaps(kilo = sum(kilo, na.rm = TRUE))

    # replace NA ----
    pedestrian %>%
      group_by(Sensor) %>%
      fill_gaps(Count = as.integer(median(Count)))

[Package tsibble version 0.7.0 Index]