I have two time series which I would like to merge: activity
is (almost) regular with 5 minute timesteps, the other, temperature
, is an irregular time series, with timestamps which do not at any point match the timestamps in activity
.
I would like add the column "temperature$temp" to the dataset activity
, with NAs for times at which there are no temp records, and the actual temp records assigned to the closest timestamp in activity
. (Alternatively to the closest prior or following timestamp).
Previously I used the approxfun function to interpolate the temperature data to match the activity timeseries, however this is not ideal and I would like to include only temperatures which were actually recorded.
I have so far been unable to modify the solutions of similar-seeming timeseries questions posted on stack overflow and elsewhere because they either assume that the timeseries will match at some times, or they aim for output which merges the time series so that the timestamps of both data sets are included, neither of which is the case here.
activity <- structure(list(Date = structure(c(1350542219, 1350542519, 1350542819,
1350543119, 1350543419, 1350543719, 1350544019, 1350544319, 1350544619,
1350544919, 1350545219, 1350545519, 1350545819, 1350546119, 1350546419,
1350546719, 1350547019, 1350547319, 1350547619), class = c("POSIXct",
"POSIXt"), tzone = "GMT"), Activity = c(300, 300, 300, 300, 300,
300, 300, 207, 0, 0, 0, 0, 153, 300, 300, 300, 300, 300, 300)), .Names = c("Date",
"Activity"), row.names = 1220:1238, class = "data.frame")
temperature <- structure(list(Date = structure(c(1350543180, 1350547140), class = c("POSIXct",
"POSIXt"), tzone = "GMT"), temp = c(12.625, 12.5)), .Names = c("Date",
"temp"), row.names = 2:3, class = "data.frame")
output <- structure(list(Date = structure(c(1350542219, 1350542519, 1350542819,
1350543119, 1350543419, 1350543719, 1350544019, 1350544319, 1350544619,
1350544919, 1350545219, 1350545519, 1350545819, 1350546119, 1350546419,
1350546719, 1350547019, 1350547319, 1350547619), class = c("POSIXct",
"POSIXt"), tzone = "GMT"), Activity = c(300, 300, 300, 300, 300,
300, 300, 207, 0, 0, 0, 0, 153, 300, 300, 300, 300, 300, 300),
temp = c(NA, NA, NA, 12.625, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 12.5, NA)), .Names = c("Date", "Activity",
"temp"), row.names = 1220:1238, class = "data.frame")
I would greatly appreciate any help or advice you could give me.