Skip to content

Instantly share code, notes, and snippets.

@eparikh
Created February 1, 2017 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eparikh/f61ced4d0c23c51d89d0fa63a393f3eb to your computer and use it in GitHub Desktop.
Save eparikh/f61ced4d0c23c51d89d0fa63a393f3eb to your computer and use it in GitHub Desktop.
This is the function I wrote to calculate the mean differences which returns the difference data and the final mean difference for a statistic.
#### MEANDIFF FUNCTION ####
getDiff <- function(barPlotData, statistic, roundPlaces){
#use dplyr and tidyr to format the data
data <- barPlotData %>%
select(Year=yearID, madePlayoffs, one_of(statistic)) %>%
spread_(key = "madePlayoffs", value = statistic) %>%
summarise(Difference=Yes-No)
#rotate dataframe from vertical to horizontal to place above bar plot
data <- as.data.frame(data %>% spread(key=Year, value = Difference))
rownames(data) <- "(Playoff - Nonplayoff)"
#find row mean and round to supplied number of decimal points
m <- apply(data, 1, mean)[[1]]
data <- round(data, roundPlaces)
return(list(
data = data,
mean = m
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment