Skip to content

Instantly share code, notes, and snippets.

View andrewbtran's full-sized avatar

Andrew Tran andrewbtran

View GitHub Profile
@andrewbtran
andrewbtran / sort
Created June 12, 2015 01:25
Sort by descending
#Sort by column TOTAL.EARNINGS descending
earnings <- earnings[order(-earnings$TOTAL.EARNINGS),]
@andrewbtran
andrewbtran / math
Created June 12, 2015 01:35
math in R
#Create new column with a formula (Convert OT column into numeric first)
earnings$OVERTIME <- gsub("\\$", "", earnings$OVERTIME)
earnings$OVERTIME <- as.numeric(earnings$OVERTIME)
#FORMULA TIME (If the columns were numbers in the first place, you could skip the steps above)
earnings$Total.minus.OT <- earnings$TOTAL.EARNINGS - earnings$OVERTIME
@andrewbtran
andrewbtran / subset
Created June 12, 2015 01:37
subset in r
#Filter out a column (in R, it's called "subset")
fire_dept <- subset(earnings, DEPARTMENT.NAME=="Boston Fire Department")
#Calculations on columns
earnings_total <- sum(earnings$TOTAL.EARNINGS)
earnings_avg <- mean(earnings$TOTAL.EARNINGS)
earnings_median <- median(earnings$TOTAL.EARNINGS)
@andrewbtran
andrewbtran / datatocolumns
Created June 12, 2015 01:45
Data to Columns R
##DATA TO COLUMNS IN R
#Create new column based on NAME column by deleting after comma
earnings$Last.Name <- sub(",.*","",earnings$NAME)
earnings$First.Name <- sub(".*,","",earnings$NAME)
#Create Middle name column based on First.Name column by deleting before space
#This makes an array out of the total number of observations in earnings
earnings_list <- 1:nrow(earnings)
@andrewbtran
andrewbtran / pivottable
Last active August 29, 2015 14:22
quick pivot table r
#Simple Pivot table to count number of employees per Department
Department_Workers <- data.frame(table(earnings$DEPARTMENT.NAME))
#Sort it
Department_Workers <- Department_Workers[order(-Department_Workers$Freq),]
#Rename Columns
colnames(Department_Workers) <- c("Department", "Employees")
#Check it
@andrewbtran
andrewbtran / advanced pivot table r
Created June 12, 2015 01:53
Advanced pivot table r
#Advanced calculations
income <- tapply(earnings$TOTAL.EARNINGS, earnings$DEPARTMENT.NAME, sum)
#Convert the table into a dataframe
income <- data.frame(income)
#Create a column based on row names
income$Department <- rownames(income)
#Need the column of rown names to merge it with the department workers count
@andrewbtran
andrewbtran / saveascsv
Created June 12, 2015 01:55
save file r
head(merged)
#Save it as a csv
write.csv(merged, "merged.csv")
@andrewbtran
andrewbtran / LeafletJS intro
Created June 22, 2015 03:17
Leaflet: Loading in Javascript and CSS libraries into the HTML
<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>
</head>
<body>
</body>
</html>
@andrewbtran
andrewbtran / LeafletJS R intro
Last active August 29, 2015 14:23
Leaflet: Loading R library
install.packages("leaflet")
library(leaflet)
install.packages("dplyr")
library(dplyr)