Skip to content

Instantly share code, notes, and snippets.

@deanmarchiori
Created August 13, 2020 06:06
Show Gist options
  • Save deanmarchiori/99463a12fff85dfc9aea81d969a8dc9a to your computer and use it in GitHub Desktop.
Save deanmarchiori/99463a12fff85dfc9aea81d969a8dc9a to your computer and use it in GitHub Desktop.
Turning implicit missing values into explicit missing values.
# tidyr::complete() & tidyr::full_seq() -----------------------------------
# Turning implicit missing values into explicit missing values.
# Bonus: Filling in gaps in a date range
library(tidyr)
library(tibble)
library(dplyr)
# Making up some observations from two weather stations.
# Some fields are nested and shouldn't be crossed e.g. station and id.
# Other observations are missing days with no data.
weather <- tibble(
weather_station_id = c(123, 123, 123, 123, 456, 456, 456),
weather_station_name = c("Sydney", "Sydney", "Sydney", "Sydney", "Melbourne",
"Melbourne", "Melbourne"),
dates = c("2020-01-01", "2020-01-02", "2020-01-04", "2020-01-07",
"2020-01-02", "2020-01-04", "2020-01-07"),
temp = c(29, 31, 27, 24, 32, 34, 35),
) %>%
mutate(dates = as.Date(dates))
weather
# Nesting the id and station name, expand()ing dates, but rather than
# use the dates present in the data we want to fill in the entire date sequence.
weather %>%
complete(nesting(weather_station_id, weather_station_name),
dates = full_seq(dates, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment