Skip to content

Instantly share code, notes, and snippets.

View chris-prener's full-sized avatar

Chris Prener chris-prener

View GitHub Profile
# batch edit column names based on a particular phrase
# store data in global environment, modify
mpg <- ggplot2::mpg
colnames(mpg) <- paste("depression", colnames(mpg), sep = "_")
# as a function
rename_by_phrase <- function(.data, pattern, replacement){
# modify variable names based on input
@chris-prener
chris-prener / albers_anchorage.R
Last active April 21, 2021 00:36
Locate Anchorage, AK using the albersusa package for R
# find where to locate Anchorage, AK using the albersusa package
# dependencies
library(albersusa) # composite projection
library(dplyr) # data wrangling
library(ggplot2) # mapping
library(mapview) # preview
library(sf) # spatial data
# load composite map
---
title: "SOC 1120-01"
subtitle: "Fall 2019 Attendance"
author: "Christopher Prener, Ph.D."
institute: "Saint Louis University"
date: "2019/08/19 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
# dependencies
library(dplyr)
library(googledrive)
library(lubridate)
library(readr)
library(stringr)
# download form data
forms <- drive_find(pattern = "FA19-01")
@chris-prener
chris-prener / bivariateMapping.R
Last active August 28, 2019 05:18
Adaptation of Grossenbacher/Zehr Bivariate Tutorial
# Bivariate Choropleth Map
## this is based on Timo Grossenbacher and Angelo Zehr's tutorial -
## https://timogrossenbacher.ch/2019/04/bivariate-maps-with-ggplot2-and-sf/
# dependencies
library(dplyr) # data wrangling
library(ggplot2) # plotting
library(purrr) # iteration
library(tidyr) # data wrangling
@chris-prener
chris-prener / parse-bocks.R
Last active March 9, 2019 02:08
Parse Census Bureau address range data
# Parse Census Bureau address range data
# Problem:
# We need a way to match incidents (that have address-level data) to the blockface they occur on.
# By blockface, I mean the houses on either side of a street between two cross streets (i.e.
# the 100-block of Main Street between 1st and 2nd Avenues).
#
# Typically, census block and city block shapefiles do not represent a blockface. Instead,
# they have parts of up to four different streets, typically representing half of a blockface
# for each of the included streets. So a block bounded by Main Street on the south, 1st Avenue
@chris-prener
chris-prener / rename-files.R
Last active February 17, 2019 01:45
Rename files using purrr+stringr+fs
# These two functions address a common problem in my work - files named "monthYYYY.CSV.html" -
# e.g. "August2018.CSV.html". The prep_dir() function creates a vector of filenames in a given directory
# and then iterates over each using purrr::map(). The map() function calls our second function, edit_filename().
# For each time it is called, edit_filename() uses the magic of stringr and fs (okay, it isn't really magic)
# to rename the file appropriately.
# dependencies:
# install.packages(c("fs", "magrittr", "purrr", "stringr"))
# include in script:
@chris-prener
chris-prener / leaflet-example.R
Last active February 12, 2019 16:24
Script for Creating Leaflet Map of Population Density in St. Louis
# dependencies
library(dplyr) # data wrangling
library(classInt) # calculate breaks
library(leaflet) # leaflet maps
library(sf) # spatial data tools
library(stringr) # work with strings
# load data and re-project to WGS 1984
st_read("STL_DEMOS_Nhoods.shp", stringsAsFactors = FALSE) %>%
st_transform(crs = 4326) -> popChange
@chris-prener
chris-prener / examBulder.R
Created February 10, 2019 21:28
Script for Creating Multiple Choice Exams
# Workflow for Creating Multiple Choice Exams
# This process creates a pseudo-random sample of correct answers, and then generates
# an exam based on a specified number of questions assigned to different sections of
# the course.
# Install Dependencies
# install.packages(c("dplyr", "purrr", "readr", "rlang"))
# Create
@chris-prener
chris-prener / iterating_strings.R
Created November 27, 2018 22:32
Detecting Multiple Patterns in Strings, Iteration
library(dplyr)
library(stringr)
# create test data
orders <- data.frame(
id = c(1,2,3,4),
items = c("Apples and Turkey", "Bacon and Crackers", "Bananas and Juice", "Pecan Pie and Ice Cream"),
stringsAsFactors = FALSE
)