goal
I want to interpolate within a group in a data frame. This will give me an arbitrary number of intermediate points for each group within the data frame.
Minimal working example
I have a dataframe like:
OldDataFrame <- data.frame(ID = c(1,1,1,2,2,2), time = c(1,2,3,1,2,3), Var1 = c(-0.6 , 0.2, -0.8 , 1.6 , 0.3 , -0.8), Var2 = c(0.5 , 0.7, 0.6 , -0.3 , 1.5 , 0.4) )
I want to get something like this:
TimeInterpolateByGroup <- function(DataFrame, GroupingVariable, TimeVariable, TimeInterval){
It would be convenient if I did not need to specify columns for this, and it could work automatically in every numerical column, for example numcolwise in plyr
So that I can apply it like this:
NewDataFrame = TimeInterpolateByGroup(DataFrame = OldDataFrame, GroupingVariable = "ID", TimeVariable = "time", TimeInterval = 0.25)
to get a NewDataFrame like:
NewDataFrame = data.frame(ID = c( 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 ), time = c( 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3 ), Var1 = c( -0.6, -0.4, -0.2, 0, 0.2, -0.05, -0.3, -0.55, -0.8, 1.6, 1.275, 0.95, 0.625, 0.3, 0.025, -0.25, -0.525, -0.8 ), Var2 = c( 0.5, 0.55, 0.6, 0.65, 0.7, 0.675, 0.65, 0.625, 0.6, -0.3, 0.15, 0.6, 1.05, 1.5, 1.225, 0.95, 0.675, 0.4 ))
Or in the form of an image I want:

A related question that doesn't quite work
Interpolate variables on subsets of a data block
- Using an approach like
plyr seems to be in the right direction, but with an intricate example and without the ability to have an arbitrary number of intermediate interpolation points. This is important for an animation application (see below), where I'm not sure how many intermediate time points I will need to get a smooth animation.
Some other answers use a time series approach, but this does not allow segmentation by groups.
I also considered using a longitudinal data packet, but this seems unnecessarily complex for what should be a simple problem.
Desired Application
I want to have a graph of xy Var1 and Var2 with points that are each entry point at time = 1. Then I want to use the animate package to see how the points move with time. To do this smoothly, I need all coordinate sets for intermediate points in time.