如何按时间间隔匹配数据帧?

发布于 2024-10-01 19:28:17 字数 1449 浏览 0 评论 0原文

这是我从数据记录器导入原始数据时经常出现的问题。温度记录仪设置为每十分钟记录一次温度,并且设置单独的气体记录仪来记录最后十分钟间隔内使用的气体。我想将这两个记录器的数据合并到一个数据框中进行绘图和分析,但时间并不完全一致。我希望每十分钟的时间段在数据框中包含一行,日期时间显示该时间段的开始。

温度记录器数据如下所示:

 日期时间 温度
2010-09-30 06:58:53 78.996
2010-09-30 07:08:53 78.645
2010-09-30 07:18:53 78.514
2010-09-30 07:28:53 79.173
2010-09-30 07:38:53 78.602

气体记录仪数据如下所示:

 日期时间气体
2010-09-30 13:45:00 0
2010-09-30 13:55:00 1
2010-09-30 14:05:00 0
2010-09-30 14:15:00 4
2010-09-30 14:25:00 2

我想以十分钟的间隔合并两个数据帧,以便合并后的数据如下所示:

 日期时间 温度 气体  
2010-09-30 13:40:00 不适用 0
2010-09-30 13:50:00 78.996 1
2010-09-30 14:00:00 78.645 0
2010-09-30 14:10:00 78.514 4
2010-09-30 14:20:00 79.173 2
2010-09-30 07:38:53 78.602 北美

这是获取这两个数据框的一些代码:

temps <- data.frame(datetime=c("2010-09-30 06:58:53",
"2010-09-30 07:08:53","2010-09-30 07:18:53",
"2010-09-30 07:28:53","2010-09-30 07:38:53"),
 temperature=c(78.996,78.645,78.514,79.173,78.602),
stringsAsFactors=FALSE)
temps$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")
gas <- data.frame(datetime=c("2010-09-30 13:45:00",
"2010-09-30 13:55:00","2010-09-30 14:05:00",
"2010-09-30 14:15:00","2010-09-30 14:25:00"),
gas=c(0,1,0,4,2),stringsAsFactors=FALSE)
gas$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")

This is a problem that comes up often when I am importing raw data from data loggers. A temperature logger is set to record temperature every ten minutes, and a separate gas logger is set to record gas used in the last ten minute interval. I want to combine the data from these two loggers into a single data frame for plotting and analysis, but the times are not exactly aligned. I want to have one row in the data frame for each ten minute period, with the datetime showing the beginning of the time period.

The temperature logger data looks like:

           datetime temperature
2010-09-30 06:58:53 78.996
2010-09-30 07:08:53 78.645
2010-09-30 07:18:53 78.514
2010-09-30 07:28:53 79.173
2010-09-30 07:38:53 78.602

The gas logger data looks like:

           datetime gas
2010-09-30 13:45:00  0
2010-09-30 13:55:00  1
2010-09-30 14:05:00  0
2010-09-30 14:15:00  4
2010-09-30 14:25:00  2

I want to combine the two data frames on ten minute intervals, so that the combined data looks like:

           datetime temperature gas  
2010-09-30 13:40:00 NA          0
2010-09-30 13:50:00 78.996      1
2010-09-30 14:00:00 78.645      0
2010-09-30 14:10:00 78.514      4
2010-09-30 14:20:00 79.173      2
2010-09-30 07:38:53 78.602      NA

Here's some code to get these two data frames:

temps <- data.frame(datetime=c("2010-09-30 06:58:53",
"2010-09-30 07:08:53","2010-09-30 07:18:53",
"2010-09-30 07:28:53","2010-09-30 07:38:53"),
 temperature=c(78.996,78.645,78.514,79.173,78.602),
stringsAsFactors=FALSE)
temps$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")
gas <- data.frame(datetime=c("2010-09-30 13:45:00",
"2010-09-30 13:55:00","2010-09-30 14:05:00",
"2010-09-30 14:15:00","2010-09-30 14:25:00"),
gas=c(0,1,0,4,2),stringsAsFactors=FALSE)
gas$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-10-08 19:28:17

xts 中使用 align.time

library(xts)
xTemps <- align.time(xts(temps[,2],as.POSIXct(temps[,1])), n=600)
xGas <- align.time(xts(gas[,2],as.POSIXct(gas[,1])), n=600)
merge(xTemps,xGas)

Use align.time in xts.

library(xts)
xTemps <- align.time(xts(temps[,2],as.POSIXct(temps[,1])), n=600)
xGas <- align.time(xts(gas[,2],as.POSIXct(gas[,1])), n=600)
merge(xTemps,xGas)
何时共饮酒 2024-10-08 19:28:17

使用 zooxts 来保存您的数据 - 他们的 merge() 函数将为您完成此操作。您可以在这里查找以前的答案。 zoo 文档也有很多相关示例。

这是一个模拟示例:

> set.seed(42)
> temps <- zoo(78+rnorm(5), 
+              order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> gas <- zoo(sample(1:5,5), 
+            order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> merge(temps,gas)
                     temps gas
2010-09-30 06:59:47 78.048  NA
2010-09-30 06:59:49     NA   5
2010-09-30 07:00:44 76.895  NA
2010-09-30 07:00:48     NA   1
2010-09-30 07:00:55     NA   3
2010-09-30 07:01:01 78.539  NA
2010-09-30 07:01:23     NA   2
2010-09-30 07:01:51 78.580  NA
2010-09-30 07:01:57     NA   4
2010-09-30 07:02:29 77.342  NA
> na.locf(merge(temps,gas))
                     temps gas
2010-09-30 06:59:49 78.048   5
2010-09-30 07:00:44 76.895   5
2010-09-30 07:00:48 76.895   1
2010-09-30 07:00:55 76.895   3
2010-09-30 07:01:01 78.539   3
2010-09-30 07:01:23 78.539   2
2010-09-30 07:01:51 78.580   2
2010-09-30 07:01:57 78.580   4
2010-09-30 07:02:29 77.342   4
> 

Use either one of zoo or xts to hold your data -- their merge() function will do this for you. You can look for previous answers here. The zoo documentation has also lots of relevant examples.

Here is a mock-up example:

> set.seed(42)
> temps <- zoo(78+rnorm(5), 
+              order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> gas <- zoo(sample(1:5,5), 
+            order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> merge(temps,gas)
                     temps gas
2010-09-30 06:59:47 78.048  NA
2010-09-30 06:59:49     NA   5
2010-09-30 07:00:44 76.895  NA
2010-09-30 07:00:48     NA   1
2010-09-30 07:00:55     NA   3
2010-09-30 07:01:01 78.539  NA
2010-09-30 07:01:23     NA   2
2010-09-30 07:01:51 78.580  NA
2010-09-30 07:01:57     NA   4
2010-09-30 07:02:29 77.342  NA
> na.locf(merge(temps,gas))
                     temps gas
2010-09-30 06:59:49 78.048   5
2010-09-30 07:00:44 76.895   5
2010-09-30 07:00:48 76.895   1
2010-09-30 07:00:55 76.895   3
2010-09-30 07:01:01 78.539   3
2010-09-30 07:01:23 78.539   2
2010-09-30 07:01:51 78.580   2
2010-09-30 07:01:57 78.580   4
2010-09-30 07:02:29 77.342   4
> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文