Learn R Programming

tsibble (version 0.5.3)

fill_na: Turn implicit missing values into explicit missing values

Description

Turn implicit missing values into explicit missing values

Usage

fill_na(.data, ...)

# S3 method for tbl_ts fill_na(.data, ..., .full = FALSE)

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.

.full

FALSE to insert NA for each key within its own period. TRUE to fill NA over the entire time span of the data (a.k.a. fully balanced panel).

See Also

count_gaps, 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, .full = TRUE)
full_harvest <- fill_na(harvest, .full = FALSE)
full_harvest

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

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

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

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

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

Run the code above in your browser using DataLab