# 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