I'm fairly sure that the code below gives the correct answer, except for a tiny level of numerical imprecision due to the use of the approx() function. The basic idea is to use ddply to split and combine data frames, and approx to do the interpolation.
library(plyr)
# time_interpolate is a helper function for TimeInterpolateByGroup
# that operates on each of the groups. In the input to this function,
# the GroupingVariable column of the data frame should be single-valued.
# The function returns a (probably longer) data frame, with estimated
# values for the times specified in the output_times array.
time_interpolate <- function(data_frame,
GroupingVariable,
time_var,
output_times) {
input_times <- data_frame[, time_var]
exclude_vars <- c(time_var, GroupingVariable)
value_vars <- setdiff(colnames(data_frame), exclude_vars)
output_df <- data.frame(rep(data_frame[1,GroupingVariable], length(output_times)), output_times)
colnames(output_df) <- c(GroupingVariable, time_var)
for (value_var in value_vars) {
output_df[,value_var] <- approx(input_times, data_frame[, value_var], output_times)$y
}
return(output_df)
}
# A test for time_interpolate
time_interpolate(OldDataFrame[1:3,], "ID" , "time", seq(from=1, to=3, by=0.25))
TimeInterpolateByGroup <- function(DataFrame,
GroupingVariable,
TimeVariable,
TimeInterval){
min_time <- min(DataFrame[, TimeVariable])
max_time <- max(DataFrame[, TimeVariable])
output_times <- seq(from=min_time, to=max_time, by=TimeInterval)
ddply(DataFrame,
GroupingVariable,
time_interpolate,
GroupingVariable=GroupingVariable,
time_var=TimeVariable,
output_times=output_times)
}