-
-
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.
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
#### 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