Reputation: 89
So basically I have a large data frame to two columns, one time column and one size column. I then have another data frame with just one time column. I want to interpolate the times from the data frame with just times into the other data frame, and I then want to return the sizes corresponding to the time that was just interpolated.
Example:
Table 1:
Time
2
4
Table 2:
Time Size
1 40
3 50
5 30
I basically want to interpolate the "2" from table 1 in between the "1" and "3" in table two, and return the sizes "40" and "50"
How do I do this using the approx function?
Upvotes: 1
Views: 4972
Reputation: 269526
Try this:
> Table1 <- data.frame(Time = c(2, 4))
> Table2 <- data.frame(Time = c(1, 3, 5), size = c(40, 50, 30))
> approx(Table2$Time, Table2$size, xout = Table1$Time, method = "constant")
$x
[1] 2 4
$y
[1] 40 50
ADDED. This can also be done using the zoo package like this:
> library(zoo)
> z2 <- read.zoo(Table2, FUN = identity)
> na.approx(z2, xout = Table1$Time, method = "constant")
2 4
40 50
In the future please write out your input data in R so it can simply be copied and pasted into a running R session as we have done above.
Upvotes: 3