dx = data.frame(
  Day = c(3, 3, 4, 4, 5, 6, 7, 7, 7),
  Name = c("Amy", "Bob", "Jim", "Amy", "Bob", "Jim", "Amy", "Bob", "Jim"),
  Result = c(11:19)
)
   Day Name Result
   3  Amy     11
   3  Bob     12
   4  Jim     13
   4  Amy     14
   5  Bob     15
   6  Jim     16
   7  Amy     17
   7  Bob     18
   7  Jim     19
Here is a table that shows the trial results of 3 subjects on specific days. How can I create a summary table that shows the average latest-observed result before that day, (as a comparison for the upcoming results)? For example, the average latest-observed result for Day 6 would be Amy's Day 4, Bob's Day 5, Jim's Day 4. For Day 7, it would be Amy/4, Bob/5, Jim/6.
This is the solution I have but a for loop seems inefficient if I were to have thousands of days and subjects.
output = data.frame(Day = unique(dx$Day)) #Extract unique days
for (i in 1:nrow(output)) {
  dfTemp = dx[dx$Day < dx2[i, "Day"],] #Find all results prior to day
  dfTemp = dfTemp[with(dfTemp, order(Name, -Day)),] #Sort descending by day
  dfTemp = dfTemp[match(unique(dfTemp$Name), dfTemp$Name),] # Take only the 1st row for each person (will be latest result due to sorting above)
  output[i, "AvgLatestResult"] = mean((dfTemp[, "Result"])) #Find mean
}
 Day AvgLatestResult 
 3   NaN  
 4   11.5
 5   13.0
 6   14.0
 7   15.0
 
     
     
    