Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 12, 2021 22:36
Show Gist options
  • Select an option

  • Save luisDVA/d8c4c4839137eb19b3554e30d9292a83 to your computer and use it in GitHub Desktop.

Select an option

Save luisDVA/d8c4c4839137eb19b3554e30d9292a83 to your computer and use it in GitHub Desktop.

Revisions

  1. luisDVA revised this gist Jan 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion restructuring-04_coalesce-and-fill.R
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ library(dplyr)
    library(tidyr)

    # import data -------------------------------------------------------------
    fishlandings <- read_csv("Restructuring Data/fish-landings.csv")
    fishlandings <- read_csv("data/fish-landings.csv")

    # fill and coalesce
    fishlandings %>%
  2. luisDVA created this gist Jan 7, 2021.
    24 changes: 24 additions & 0 deletions restructuring-04_coalesce-and-fill.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    ## %######################################################%##
    # #
    #### 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("Restructuring 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`))