# # lecture-13.r # vqv # # Created by Vincent Vu on 2011-10-10. # This work is licensed under a Creative Commons # Attribution-NonCommercial-ShareAlike 3.0 Unported License. # # masters.Rdata contains a single object, 'masters2011', that is a list with # 3 components: 'leaderboard', 'scorecard', 'course' load('masters2011.RData') runningTotal <- function(df, par = masters2011$course$par) { # Reorder the rows of the data frame by the round number # (so that the scores are in chronological order) # and extract the scores as a matrix x <- as.matrix(df[order(df$round), 1:18]) n <- nrow(x) # Convert from an 18 x n matrix to a vector of length 18*n, rowwise x <- as.vector(t(x)) # Calculate the running over/under score x <- cumsum(x - rep(par, n)) return(x) } # ================================================================== # = Look at all players that made the cut (played in all 4 rounds) = # ================================================================== madecut <- subset(masters2011$leaderboard, position != 'CUT')$player df <- subset(masters2011$scorecard, player %in% madecut) df <- droplevels(df) # =================================== # = Split, apply, combine with plyr = # =================================== library(plyr) scores <- daply(df, 'player', runningTotal) countPlayers <- function(x) { data.frame(count = length(unique(x$player))) } ddply(df, 'country', countPlayers) # ============== # = Strategy 2 = # ============== scorePlayer <- function(x) { par <- masters2011$course$par data.frame(score = sum(x[1:18] - masters2011$course$par)) } scores <- ddply(df, c('player', 'round'), scorePlayer) scores <- merge(df, scores) daply(scores, c('country', 'round'), function(x) median(x$score))