# 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