Skip to content

Instantly share code, notes, and snippets.

View andrewbtran's full-sized avatar

Andrew Tran andrewbtran

View GitHub Profile
@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 / 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)
#Calculations on columns
earnings_total <- sum(earnings$TOTAL.EARNINGS)
earnings_avg <- mean(earnings$TOTAL.EARNINGS)
earnings_median <- median(earnings$TOTAL.EARNINGS)
@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")
@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 / sort
Created June 12, 2015 01:25
Sort by descending
#Sort by column TOTAL.EARNINGS descending
earnings <- earnings[order(-earnings$TOTAL.EARNINGS),]
@andrewbtran
andrewbtran / numeric
Created June 12, 2015 01:22
change to numeric format r
#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)
@andrewbtran
andrewbtran / str
Created June 12, 2015 01:12
structure r
# Investigate the structure of the dataframe
str(earnings)
@andrewbtran
andrewbtran / firstfivelastfive
Created June 12, 2015 01:05
head and foot in R
# Look at the first five rows in the Console
head(earnings)
# Alternatively, to look at the last five rows
foot(earnings
@andrewbtran
andrewbtran / nrow
Created June 12, 2015 01:03
how many rows in R?
# How many rows?
nrow(earnings)