以 10 分钟为间隔连接和绘制不同时间的数据

Join and plot data with different times in 10 minute interval

我在 Access 数据库中有 3 个 table 具有相同的列名(TempDateTemp),但时间戳不同。数据以 10 分钟为间隔收集,但每个记录设备的开始时间不同。我想将它们合并为一个 table,其中每个 table(temp1、temp2、temp3)都有一个 TempDate 和一个 Temp 列。

我需要有关如何在 Access 或 R 中执行此操作的帮助。我已经开始将 R 与 MySQL 代码一起使用,但我对它还是很陌生。提前致谢。最终,我想将此数据连接到另一个带有同一日期时间戳的数据框。如果有人可以告诉我如何告诉它按时间间隔分组,我想我可以做到这一点。最后使用 ggplot

绘图

数据

    temp1<-data.frame(TempDate=c("2020/08/11 07:13:01","2020/08/11 07:23:01","2020/08/11 07:33:01","2020/08/11 07:43:01"),Temperature=c(1.610,-1.905,-1.905,-0.901))

temp2<-data.frame(TempDate=c("2020/08/11 07:10:01","2020/08/11 07:20:01","2020/08/11 07:30:01","2020/08/11 07:40:01"),Temperature=c(15.641,15.641,15.641,15.641))

temp3<-data.frame(TempDate=c("2020/08/11 07:19:01","2020/08/11 07:29:01","2020/08/11 07:39:01","2020/08/11 07:49:01"),Temperature=c(2.062,3.573,4.076,4.579))

> temp3 #as example
             TempDate Temperature
1 2020/08/11 07:19:01       2.062
2 2020/08/11 07:29:01       3.573
3 2020/08/11 07:39:01       4.076
4 2020/08/11 07:49:01       4.579

#what I want row 1 is temps recorded from 07:10:00-07:29:59, etc
> 
             TempDate    Temp1    Temp2   Temp3
1 2020/08/11 07:10:00    1.610    15.641   2.062
2 2020/08/11 07:20:00    -1.905   15.641   3.573
3 2020/08/11 07:30:00    -1.905   15.641   4.076
4 2020/08/11 07:40:00    -1.901   15.641   4.579

更新: 感谢 Ben 提供了很好的答案,让我开始解决这个问题。在问另一个问题时,建议 floor_date 。这段代码比@Ben 的 cut 函数更适合我的数据。使用 cut 时,我会得到以 9 (12:19) 而不是 0 (12:10) 结尾的时间。我还在 cut 函数中尝试了 TempDate+60 ,但是有些日期会在接下来的 10 分钟间隔内得到一个时间。下面的代码更准确。

library(lubridate)    
tempdata<-bind_rows(burrow=burrow,shade=shade,sun=sun,.id='Series') %>%
       mutate(TempDate = as.POSIXct(TempDate, tz="UTC"),
         TimeStamp = floor_date(TempDate, unit='10 mins'),
         TimeStamp = as.POSIXct(TimeStamp, tz="UTC")) %>%
       filter(TimeStamp > as.POSIXct("2020-08-12 13:29:00", tz="UTC")) %>%
       select(Series, Temperature,TimeStamp) %>%
       arrange(TimeStamp)

在 Access (VBA) 中,您可以像这样向下舍入时间:

texttime = "2020/08/11 07:19:01"
truetime = DateValue(texttime) + TimeSerial(Hour(CDate(texttime)), (Minute(CDate(texttime)) \ 10) * 10, 0)

' Result:
' 2020-11-08 07:10:00 

但是,如何在R中实现这个,我不知道。

在 R 中,您可以使用 tidyverse 方法执行以下操作。

首先,您可以使用 bind_rows 将所有数据框放在一起,并添加一个 source 列,其中包含这些温度来自的数据框的名称,或最终结果中的目标列。

然后,确保您的 TempDatePOSIXct。您可以使用 cut 将日期时间设置为 10 分钟间隔。

在这一点上,我会考虑保留结果,以便用 ggplot2 绘图。通常最好保留“长”格式而不是“宽”格式。但是,如果您想要“宽”格式,则可以使用 tidyr.

中的 pivot_wider
library(dplyr)
library(tidyr)

bind_rows(temp1 = temp1, temp2 = temp2, temp3 = temp3, .id = 'source') %>%
  mutate(TempDate = as.POSIXct(TempDate),
         NewTempDate = cut(TempDate, breaks = "10 min")) %>%
  pivot_wider(id_cols = NewTempDate, names_from = source, values_from = Temperature)

输出

  NewTempDate          temp1 temp2 temp3
  <fct>                <dbl> <dbl> <dbl>
1 2020-08-11 07:10:00  1.61   15.6  2.06
2 2020-08-11 07:20:00 -1.90   15.6  3.57
3 2020-08-11 07:30:00 -1.90   15.6  4.08
4 2020-08-11 07:40:00 -0.901  15.6  4.58