Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 12, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisDVA/d8c4c4839137eb19b3554e30d9292a83 to your computer and use it in GitHub Desktop.
Save luisDVA/d8c4c4839137eb19b3554e30d9292a83 to your computer and use it in GitHub Desktop.
Coalesce and Fill
## %######################################################%##
# #
#### Coalesce and Fill - Your Turn ####
# #
## %######################################################%##
# Load the fish landings data 'fish-landings.csv'
# Fill the 'Fish' and 'Lake' columns
# Reorder the numeric variables ('Comission reported total' first)
# create a new column, coalescing the three numeric variables
# load packages -----------------------------------------------------------
library(readr)
library(dplyr)
library(tidyr)
# import data -------------------------------------------------------------
fishlandings <- read_csv("data/fish-landings.csv")
# fill and coalesce
fishlandings %>%
fill(Fish, Lake) %>%
select(Fish, Lake, Month, `Commission reported total`, `Official total`, `Previous year total`) %>%
mutate(new_total = coalesce(`Commission reported total`, `Official total`, `Previous year total`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment