Skip to content

Instantly share code, notes, and snippets.

View andrewbtran's full-sized avatar

Andrew Tran andrewbtran

View GitHub Profile
@andrewbtran
andrewbtran / index.html
Created November 18, 2015 04:13
Highcharts starter
<html>
<head>
<title>Highcharts Walkthrough</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
@andrewbtran
andrewbtran / gist:cd351d87303669110fd8
Created August 21, 2015 01:09
Pivot tables advanced but simplified with dplyr and pipes
gender_df2 <- payroll %>%
group_by(AGENCY_DESCRIPTION, gender) %>%
summarize(employees=n(), avg.salary=mean(TOTAL_AMT), median.salary=median(TOTAL_AMT)) %>%
mutate(percent=round((employees/sum(employees)*100))) %>%
arrange(desc(percent))
@andrewbtran
andrewbtran / gist:1034b401076c09640aa1
Created August 21, 2015 00:53
pivot tables using dplyr
gender_df <- payroll %>%
group_by(gender) %>%
summarize(employees=n(), avg.salary=mean(TOTAL_AMT), median.salary=median(TOTAL_AMT)) %>%
mutate(percent=round((employees/sum(employees)*100))) %>%
arrange(desc(percent))
@andrewbtran
andrewbtran / gist:770c651edc062ddd1eb8
Last active August 29, 2015 14:27
Gender payroll by department pivot tables r
# Same as the table() process before, but we add another variable
gender_dept <- data.frame(table(payroll$AGENCY_DESCRIPTION, payroll$gender))
colnames(gender_dept) <- c("department", "gender", "count")
# Turning a table() into a data.frame() isn't the cleanest process
# It takes some adjusting with spread() which will turn it into the structure we recognize
library(tidyr)
gender_dept <- spread(gender_dept, gender, count)
# Ok, average pay by gender in each department?
@andrewbtran
andrewbtran / gist:80561defbd9c0e64996f
Last active August 29, 2015 14:27
gender pivot table
# Like before, we're creating a pivot table and turning it into a data frame
gender <- data.frame(table(payroll$gender))
# Renaming the columns because otherwise it'd say "Var1" etc.
colnames(gender) <- c("gender", "count")
# Calculations!
gender$percent <- round((gender$count/sum(gender$count))*100, digits=2)
# Ok, let's add a column for average pay
@andrewbtran
andrewbtran / gist:d3d8e04f5c86dcfa2bb0
Last active August 29, 2015 14:27
Inferring gender from column of first names in R
# Let's do some advanced stuff.
# First we have to isolate the first name from the NAME field
payroll$first_name <- gsub(".*\\,", "", payroll$NAME)
payroll$first_name <- gsub(" .*", "", payroll$first_name)
# Bring in a library to help normalize the cases
library(stringr)
payroll$first_name <- str_to_title(payroll$first_name)
@andrewbtran
andrewbtran / Leaflet JS example
Created June 24, 2015 19:01
Leaflet JS circle map with Legend
<html>
<head>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<style>
#map-wrapper {
width: 100%;
height:400px;
position: relative;
}
@andrewbtran
andrewbtran / Pipes
Last active August 29, 2015 14:23
Pipe operator in R
# REGULAR NON-PIPING WAY
ct_only <- filter(dunk, state=="CT") # This is taking the dataframe 'dunk' that has all the locations and information on all Dunkin' Donuts across the country and creating a new one by pulling only the rows with 'CT' in the column 'state'
ct_count <- count(ct_only, city) # This is counting the number of city observations and assigning it to the variable 'ct_count'
# USING THE PIPE OPERATOR
ct_count <- dunk %>% filter(state=="CT") %>% count(city) # This is saying take the 'dunk' data frame, filter it, and then count how often 'city' appears
# Note: the filter() and count() functions are from the 'dplyr' package that you loaded earlier
@andrewbtran
andrewbtran / Adding legend to Leaflet JS
Created June 23, 2015 01:02
Add legend to Leaflet JS
<head>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<style>
#map { height: 400px; }
#map-wrapper {
width: 100%;
height:400px;
position: relative;
@andrewbtran
andrewbtran / Add legend to Leaflet R
Last active August 29, 2015 14:23
Adding legend to Leaflet R
m <- leaflet(ct) %>% addTiles('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png', attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>%
setView(-72.690940, 41.651426, zoom = 8) %>%
addCircles(~lng, ~lat, popup=ct$type, weight = 3, radius=40,
color="#ffa500", stroke = TRUE, fillOpacity = 0.8) %>%
addLegend("bottomright", colors= "#ffa500", labels="Dunkin'", title="In Connecticut")
m