I know variations of this question have been asked and answered 1,000 times. I've read every post and tried to implement what I read without success.
I have a dataframe of basketball individual game statistics for multiple players. I want to calculate 1) the "season to date" rolling median for each player (excluding the current observation) and 2) The median for he last 3 previous games (excluding the current observation)
I've included samples of what I would expect the output to be in each case.
Here's a sample of the data:
# Create Dataframe
plyr_seas = c("J.J. Redick", "Andre Drummond", "Daniel Theis", "Andre Drummond", "Daniel Theis", "J.J. Redick", "Andre Drummond", "J.J. Redick", "Andre Drummond", "J.J. Redick", "Daniel Theis", "Andre Drummond", "J.J. Redick", "Daniel Theis")
plyrOREBpmin_gm = c(0, 0.1715686275, 0.1013513514, 0.02721088435, 0.2002224694, 0, 0.1217861976, 0.03552397869, 0.1044386423, 0.03984063745, 0.03573555688, 0.2264150943, 0, 0.03719776813)
plyrGmsPlydpcs = c(0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 2, 4, 4, 3)
expected_last_3_median = c(NA, NA, NA, NA, NA, NA, NA, NA, 0.1217861976, 0, NA, 0.1044386423, 0.03552397869, 0.1013513514)
expected_overall_median = c(NA, NA, NA, 0.1715686275, 0.1013513514, 0, 0.0993897559, 0, 0.1217861976, 0, 0.1507869104, 0.1131124199, 0.01776198934, 0.1013513514)
Pbox = data.frame(plyr_seas, plyrOREBpmin_gm, plyrGmsPlydpcs, expected_last_3_median, expected_overall_median)
SOLUTION for above data! Provided by: G. Grothendieck
# Calc rolling median for last three games (excluding current)
Pbox = Pbox %>%
group_by(plyr_seas) %>%
mutate(plyrOREBrateMdnLst3 =
rollapplyr(plyrOREBpmin_gm, list(-seq(3)), median, fill = NA))
# Calc rolling median for season to date (excluding current)
Pbox = Pbox %>%
group_by(plyr_seas) %>%
mutate(plyrOREBrateMdnSeas =
rollapplyr(plyrOREBpmin_gm, seq_along(plyrGmsPlydpcs),
function(x) median(head(x, -1)), fill = NA))