R

US CO2 emissions by source

library(pacman) p_load(tidyverse, scales, ggthemes, RColorBrewer, plotly) energy_cons <- read_csv("https://www.eia.gov/totalenergy/data/browser/csv.php?tbl=T12.01") energy_cons %>% glimpse ## Observations: 8,372 ## Variables: 6 ## $ MSN <chr> "CKTCEUS", "CKTCEUS", "CKTCEUS", "CKTCEUS", "CKTCEU… ## $ YYYYMM <dbl> 197301, 197302, 197303, 197304, 197305, 197306, 197… ## $ Value <dbl> 108.289, 97.698, 97.366, 93.084, 94.346, 97.757, 10… ## $ Column_Order <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, … ## $ Description <chr> "Coal, Including Coal Coke Net Imports, CO2 Emissio… ## $ Unit <chr> "Million Metric Tons of Carbon Dioxide", "Million M… energy_cons_totals <- energy_cons %>% mutate(YYYYMMDD = paste0(YYYYMM, "01"), #get into format so can use lubridate date = lubridate::ymd(YYYYMMDD), year = lubridate::year(date), description = factor(Description), description = case_when( grepl("Coal", description) ~ "Coal", grepl("Natural Gas", description) ~ "Natural Gas", grepl("Petroleum", description) ~ "Petroleum", # grepl("Jet Fuel", description) ~ "Jet Fuel", grepl("Motor Gasoline", description) ~ "Motor Gasoline", grepl("Total Energy", description) ~ "Total Energy", TRUE ~ "Other") ) %>% group_by(year, description) %>% filter(!

Use pmap from R package purrr to combine spreadsheets

library('pacman') p_load(tidyverse, readxl, purrr) # download xlsx files from moodle put in List file <- list.files(pattern = "*.xlsx") # columns (course, group) to add to each spreadsheet course <- c("fa-18-IntAcctA", "fa-18-IntAcctA", "fa-18-AdvAcct") group <- c("12PM", "2PM", "4PM") files_in <- function(file, course, group){ file %>% read_xlsx() %>% select(firstname = First name, lastname = Last name, username = ID number, email = Email address) %>% mutate(course1 = course, group1 = group) %>% select(firstname, lastname, username, course1, group1, email) } l <- list(file, course, group ) pmap_dfr(l, files_in) %>% write_csv("enroll-fa18.