Example data:
set.seed(1) df <- data.frame(years=sort(rep(2005:2010, 12)), months=1:12, value=c(rnorm(60),NA,NA,NA,NA,
Sticking with base R, you can also try the following:
medians = sapply(split(df[1:60, 3], df[1:60, 2]), median) df[61:72, 3] = medians