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
##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
#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
#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
#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
#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
#Change column to number format (first you have to strip out the $) | |
#The $ is a special character | |
earnings$TOTAL.EARNINGS <- gsub("\\$", "", earnings$TOTAL.EARNINGS) | |
#Function to change the format to numeric | |
earnings$TOTAL.EARNINGS <- as.numeric(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
# Investigate the structure of the dataframe | |
str(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
# Look at the first five rows in the Console | |
head(earnings) | |
# Alternatively, to look at the last five rows | |
foot(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
# How many rows? | |
nrow(earnings) |