This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> — Map data © <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 |