This file contains 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
#load the following packages | |
library(pheatmap) | |
#rename rows after attriubtes | |
rownames(data) <- data[,1] | |
#strip attribute column out | |
data2 <- data[,-1] | |
# create custom, 5-color green palette using ColorBrewer (http://colorbrewer2.org/) |
This file contains 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
Likert5 <- function(x) { | |
foo <- ifelse(x == 5, 1, ifelse(x >= 1 & x < 5, 0, NA)) | |
foo <- as.data.frame(foo) | |
names(foo) <- paste0(names(foo), "_re") | |
foo | |
} |
This file contains 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 a map of the United States and color states by values of a specific variable | |
#load libraries | |
library(maps) | |
library(ggplot2) | |
library(plyr) | |
#create a table with lowercase, full names for each state and then the variable you want to use for the fill to the right of it. Name the first column "region" | |
#merge data and states in the map plot on the "region" variable; keep data arranged by polygon |
This file contains 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
# Quick script to make a more readable list of variables and attributes for dataframes in RStudio. | |
# Gives variable number, name, first observation, and the variable's class and returns a dataframe | |
# Modeled on SPSS's Variable View | |
variable_list <- function(x) { | |
data <- x[1,] | |
data <- t(data) | |
vars <- row.names(data) | |
num <- 1:length(vars) | |
data <- as.data.frame(cbind(num,vars,data)) |
This file contains 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
convert.magic <- function(obj, type){ | |
FUN1 <- switch(type, character = as.character, numeric = as.numeric, factor = as.factor) | |
out <- lapply(obj, FUN1) | |
as.data.frame(out) | |
} |
This file contains 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
require(dplyr) | |
require(ggplot2) | |
require(gganimate) | |
war <- read.csv("https://raw.githubusercontent.com/BillPetti/General-Code-Reference/master/FanGraphs_Leaderboard_Cum_WAR.csv", header=TRUE, na.strings="NA", dec=".", stringsAsFactors = FALSE) | |
war <- war %>% group_by(playerid) %>% arrange(Age) %>% mutate(Cumulative.Off = cumsum(Off), Cumulative.Def = cumsum(Def), Cumulative.WAR = cumsum(WAR)) | |
plot <- ggplot(war, aes(Cumulative.Off, Cumulative.Def, frame = Age)) + geom_point(data = filter(war, Name == "Ted Williams"), colour = "red4", aes(size = Cumulative.WAR)) + geom_text(data = filter(war, Name == "Ted Williams"), aes(label = paste0("Williams:\n", Cumulative.WAR)), fontface = 2, size = 4.5, hjust=1, vjust=-1) + geom_point(data = filter(war, Name == "Ken Griffey Jr."), colour = "cyan3", aes(size = Cumulative.WAR)) + geom_text(data = filter(war, Name == "Ken Griffey Jr."), aes(label = paste0("Griffey, Jr.:\n", Cumulative.WAR)), fontface = 2, size = 4.5, hjust=0.5, vjust=-1) + geom_point(data = filter(war, N |
This file contains 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
is_it_opening_day <- function(x) { | |
openingday <- as.Date("2017-04-03") | |
if ((openingday - Sys.Date()) > 1) {print(paste0("Nope. We've still got ", (openingday - Sys.Date()), " days to go. Stay strong."))} else | |
if ((openingday - Sys.Date()) == 1) {print(paste0("Nope, opening night is not the same thing as opening day. We've still got 1 day to go. But there are a few games on tonight."))} | |
else | |
if ((Sys.Date() - openingday) > 1) {print(paste0("Seriously? The season started ", (Sys.Date() - openingday), " days ago. Go watch some baseball!"))} | |
else | |
if ((Sys.Date() - openingday) == 1) {print(paste0("The season started yesterday. Go watch some baseball!"))} | |
else {print("Yes! Rejoice! It's Opening Day, All's right with the world.")} | |
} |
This file contains 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
# Inspired by http://www.howmanyaltuves.com/ | |
# function takes any distance in feet and converts it to the number of Jose Altuves | |
howmanyAltuves <- function(x) { | |
altuves <- x/5.416666666666667 | |
return(paste0(round(altuves,1), " Altuves")) | |
} |
This file contains 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
require(baseballr) | |
require(dplyr) | |
dates <- data.frame(date = seq(as.Date("2016-04-05"), as.Date("2016-04-10"), by = "day")) | |
test <- dates %>% | |
group_by(date) %>% | |
do(daily_batter_bref(.$date, .$date)) %>% | |
ungroup() %>% | |
select(date, everything()) %>% | |
arrange(Name) |
This file contains 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
# Bill Petti | |
# September 2016 | |
# hex color palette for all 30 Major League Teams | |
# colors courtesy of the awesome http://jim-nielsen.com/teamcolors/ | |
mlb_team_colors <- c("Diamondbacks"="#A71930", "Braves"="#13274F", "Orioles"="#DF4601", "Red Sox"="#BD3039", "Cubs"="#CC3433", "White Sox"="#000000", "Reds"="#C6011F","Indians"="#E31937", "Rockies"="#333366","Tigers"="#0C2C56", "Astros"="#EB6E1F", "Royals"="#004687", "Angels"="#BA0021", "Dodgers"="#005A9C", "Marlins"="#0077C8", "Brewers"="#B6922E", "Twins"="#002B5C", "Mets"="#FF5910", "Yankees"="#003087", "Athletics"="#003831", "Phillies"="#E81828", "Pirates"="#FDB827", "Padres"="#7F411C", "Giants"="#8B6F4E", "Mariners"="#005C5C", "Cardinals"="#C41E3A", "Rays"="#8FBCE6", "Rangers"="#003278", "Blue Jays"="#134A8E", "Nationals"="#AB0003") |
OlderNewer