I’ve got two data frames containing timeseries (with time coded as numeric, rather than time objects; and time is unsorted). I’d like to normalize a response variable in one data frame to a response variable in another data frame. The problem is that the timepoints in the two data frames aren’t quite equivalent. So, I’ll need to merge the two data frames by the approximate match of the two time columns.
The data look like this:
df1 <- structure(list(t1 = c(3, 1, 2, 4), y1 = c(9, 1, 4, 16)), .Names = c("t1", "y1"), row.names = c(NA, -4L), class = "data.frame")
df2 <- structure(list(t2 = c(0.9, 4.1), y2 = structure(1:2, .Label = c("a", "b"), class = "factor")), .Names = c("t2", "y2"), row.names = c(NA, -2L), class = "data.frame")
The result should look like this:
t1 y1 y2
1 1 a
4 16 b
Seems like approx or approxfun would be useful, but I can’t quite see how to do it.
You can do this easily with
na.approxfrom zoo:You could do this with
approxtoo: