将数据从不规则的时间序列添加到具有5分钟时间步长的时间序列 [英] adding data from an irregular time series to a timeseries with 5-min timesteps

查看:106
本文介绍了将数据从不规则的时间序列添加到具有5分钟时间步长的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要合并的时间序列:activity(几乎)是常规的,有5分钟的时间步长,另一个,temperature,是不规则的时间序列,其时间戳在任何时候都不匹配activity中的时间戳.

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.

我想将列"temperature $ temp"添加到数据集"activity",其中包含不存在临时记录的时间的NA,并将实际的临时记录分配给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).

以前,我使用roxfun函数对温度数据进行插值以匹配活动时间序列,但这并不理想,我只想包含实际记录的温度.

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.

推荐答案

data.table中的roll=工具可以做到这一点:

The roll= facility in data.table can do this:

library(data.table)
activity.dt <- data.table(activity, key="Date")[,Date2:=Date]
temperature.dt <- data.table(temperature)

activity.dt[temperature.dt, list(Date=Date2, temp), roll=-Inf][
  activity.dt, list(Date, Activity, temp)]

给予:

                   Date Activity   temp
 1: 2012-10-18 06:36:59      300     NA
 2: 2012-10-18 06:41:59      300     NA
 3: 2012-10-18 06:46:59      300     NA
 4: 2012-10-18 06:51:59      300     NA
 5: 2012-10-18 06:56:59      300 12.625
 6: 2012-10-18 07:01:59      300     NA
 7: 2012-10-18 07:06:59      300     NA
 8: 2012-10-18 07:11:59      207     NA
 9: 2012-10-18 07:16:59        0     NA
10: 2012-10-18 07:21:59        0     NA
11: 2012-10-18 07:26:59        0     NA
12: 2012-10-18 07:31:59        0     NA
13: 2012-10-18 07:36:59      153     NA
14: 2012-10-18 07:41:59      300     NA
15: 2012-10-18 07:46:59      300     NA
16: 2012-10-18 07:51:59      300     NA
17: 2012-10-18 07:56:59      300     NA
18: 2012-10-18 08:01:59      300 12.500
19: 2012-10-18 08:06:59      300     NA

这篇关于将数据从不规则的时间序列添加到具有5分钟时间步长的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆