Learn R Programming

tsibble (version 0.1.0)

fill_na: Turn implicit missing values into explicit missing values

Description

Turn implicit missing values into explicit missing values

Usage

fill_na(.data, ...)

Arguments

.data

A data frame.

...

A set of name-value pairs. The values will replace existing explicit missing values by variable, otherwise NA. The replacement values must be of the same type as the original one. If using a function to fill the NA, please make sure that na.rm = TRUE is switched on.

See Also

case_na, tidyr::fill, tidyr::replace_na

Examples

Run this code
# NOT RUN {
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
)

# leave NA as is
fill_na(harvest)

# replace NA with a specific value
harvest %>%
  fill_na(kilo = 0L)

# replace NA using a function by variable
# enable `na.rm = TRUE` when necessary
harvest %>%
  fill_na(kilo = sum(kilo, na.rm = TRUE))

# replace NA using a function for each group
harvest %>%
  group_by(fruit) %>%
  fill_na(kilo = sum(kilo, na.rm = TRUE))

# replace NA
pedestrian %>%
  group_by(Sensor) %>%
  fill_na(
    Date = lubridate::as_date(Date_Time),
    Time = lubridate::hour(Date_Time),
    Count = as.integer(median(Count, na.rm = TRUE))
  )
# }

Run the code above in your browser using DataLab