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
#Sort by column TOTAL.EARNINGS descending | |
earnings <- earnings[order(-earnings$TOTAL.EARNINGS),] |
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
#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 |
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
#Filter out a column (in R, it's called "subset") | |
fire_dept <- subset(earnings, DEPARTMENT.NAME=="Boston Fire 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
#Calculations on columns | |
earnings_total <- sum(earnings$TOTAL.EARNINGS) | |
earnings_avg <- mean(earnings$TOTAL.EARNINGS) | |
earnings_median <- median(earnings$TOTAL.EARNINGS) |
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
##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) |
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
#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 |
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
#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 |
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(merged) | |
#Save it as a csv | |
write.csv(merged, "merged.csv") |
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> | |
</head> | |
<body> | |
</body> | |
</html> |
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
install.packages("leaflet") | |
library(leaflet) | |
install.packages("dplyr") | |
library(dplyr) |