Interpolate Variables on Data Subsets

I have a large framework that oversees research from several states for several years. Here's the data structure:

state | survey.year | time1 | obs1 | time2 | obs2 CA | 2000 | 1 | 23 | 1.2 | 43 CA | 2001 | 2 | 43 | 1.4 | 52 CA | 2002 | 5 | 53 | 3.2 | 61 ... CA | 1998 | 3 | 12 | 2.3 | 20 CA | 1999 | 4 | 14 | 2.8 | 25 CA | 2003 | 5 | 19 | 4.3 | 29 ... ND | 2000 | 2 | 223 | 3.2 | 239 ND | 2001 | 4 | 233 | 4.2 | 321 ND | 2003 | 7 | 256 | 7.9 | 387 

For each state / survey.year combination, I would like to interpolate obs2 so that its temporary location is lined up (time1, obs1).

those. I would like to split the data block into state / survey.year sections, do my linear interpolation, and then stitch the individual state / survey.year data frames back together into the main data frame.

I tried to figure out how to use plyr and Hmisc packages for this. But hold on to the ball.

Here is the code I wrote for the interpolation:

 require(Hmisc) df <- new.obs2 <- NULL for (i in 1:(0.5*(ncol(indirect)-1))){ df[,"new.obs2"] <- approxExtrap(df[,"time1"], df[,"obs1"], xout = df[,"obs2"], method="linear", rule=2) } 

But I'm not sure how to unleash plyr on this issue. Your generous advice and suggestions will be highly appreciated. Essentially - I'm just trying to interpolate "obs2" in each state / survey.year combination, so the time references match the bodies of "obs1".

Of course, if there is a slippery way to do this without calling plyr functions, then I will be open to this ...

Thanks!

0
source share
1 answer

It should be as simple as

 ddply(df,.(state,survey.year),transform, new.obs2 = approxExtrap(time1,obs1,xout = obs2, method = "linear", rule = 2)) 

But I canโ€™t promise you anything, since I donโ€™t have the most vague idea what is the point of your for loop. ( df[,"new.obs2"] rewriting df[,"new.obs2"] each time through the loop? Do you initialize the entire df data frame to NULL ? What is indirect ?)

+2
source

All Articles