【问题标题】:Use time interval to calculate mean from another data frame使用时间间隔从另一个数据帧计算平均值
【发布时间】:2017-08-31 12:39:45
【问题描述】:

我有两个数据框。一个数据帧包含一组网络(开始)和提升(结束)的时间。我需要使用这个时间段来计算我的第二个数据帧的平均 DO。我遇到了与时间匹配的问题,因为 catch 数据框中的时间是特定的,而 DO 数据框中的时间是每小时一次。我尝试匹配最接近的时间,但无法做到这一点,所以我的下一个想法是在时间间隔内使用任何东西。平均值需要特定于时间间隔内的船和网。我正在为几个非常大的数据集执行此操作,因此我需要能够循环函数而不是每一行的代码。

catchdf:

Boat    Net   Set                    Lift
Dawn    26    2016-05-19 12:20:11    2016-05-27 11:48:36
Nip     26    2016-07-28 07:25:47    2016-08-07 06:13:10
Dawn    26.3  2016-08-01 13:24:51    2016-08-03 07:48:52

dodf:

time                   DO      Boat    Net
2016-05-19 13:00:00    10.2    Dawn    26
2016-05-21 15:00:00    10.4    Dawn    26
2016-05-26 09:00:00    10.9    Dawn    26
2016-05-28 10:00:00    9.4     Dawn    26
2016-07-28 09:00:00    11.9    Nip     26
2016-07-28 19:00:00    12.4    Nip     26
2016-08-04 04:00:00    5.4     Nip     26
2016-08-01 05:00:00    13.2    Dawn    26.3
2016-08-02 16:00:00    12.3    Dawn    26.3
2016-08-05 22:00:00    2.4     Dawn    26.3

我的目标是在每个时间段的均值 DO 中插入一列。

Boat    Net   Set                    Lift                  MeanDO
Dawn    26    2016-05-19 12:20:11    2016-05-27 11:48:36   10.50
Nip     26    2016-07-28 07:25:47    2016-08-07 06:13:10   9.90
Dawn    26.3  2016-08-01 13:24:51    2016-08-03 07:48:52   12.3

这是我用来制作示例数据框的代码:

catchdf <- structure(list(Boat = c("Dawn", "Nip", "Dawn"), Net = c("26", "26", "26.3"), Set = c("2016-05-19 12:20:11", "2016-07-28 07:25:47", "2016-08-01 13:24:51"), Lift = c("2016-05-27 11:48:36", "2016-08-07 06:13:10", "2016-08-03 07:48:52")), .Names = c("Boat", "Net", "Set", "Lift"), class = "data.frame", row.names = c(NA, -3L))

dodf <- structure(list(time = c("2016-05-19 13:00:00", "2016-05-21 15:00:00", "2016-05-26 09:00:00", "2016-05-28 10:00:00", "2016-07-28 09:00:00", "2016-07-28 19:00:00", "2016-08-04 04:00:00", "2016-08-01 05:00:00", "2016-08-02 16:00:00", "2016-08-05 22:00:00"), DO = c("10.2", "10.4", "10.9", "9.4", "11.9", "12.4", "5.4", "13.2", "12.3", "2.4"), Boat = c("Dawn", "Dawn", "Dawn", "Dawn", "Nip", "Nip", "Nip", "Dawn", "Dawn", "Dawn"), Net = c("26", "26", "26", "26", "26", "26", "26", "26.3", "26.3", "26.3")), .Names = c("time", "DO", "Boat", "Net"), class = "data.frame", row.names = c(NA, -10L))

我已经在这个问题上卡了很长一段时间,一直没有取得任何进展。任何帮助将不胜感激。

【问题讨论】:

  • 感谢收看,我已编辑。
  • 我尝试匹配到最近的时间但无法匹配”是什么意思?我猜这很容易实现。您还可以为最接近的匹配添加所需的输出吗?我的意思是你想要为每个 time 值或每个 SetLift 或某种组合匹配?
  • 所以我最初的想法是匹配集合并提升到dodf中最接近的时间,然后取这两个时间段之间所有DO值的平均值。我认为您详细说明使用时间>设置,时间

标签: r datetime mean


【解决方案1】:

这是可能的 data.table 解决方案。首先,我们将修复列的格式

library(data.table) #v1.10.4
cols <- c("Set", "Lift")
setDT(catchdf)[, (cols) := lapply(.SD, as.POSIXct), .SDcols = cols]
setDT(dodf)[, `:=`(time = as.POSIXct(time), DO = as.numeric(DO))]

然后我们可以在计算平均值的同时在数据集之间进行非等连接

dodf[catchdf, .(MeanDO = mean(DO)), on = .(Boat, Net, time > Set, time < Lift), by = .EACHI]
#    Boat  Net                time                time MeanDO
# 1: Dawn   26 2016-05-19 12:20:11 2016-05-27 11:48:36   10.5
# 2:  Nip   26 2016-07-28 07:25:47 2016-08-07 06:13:10    9.9
# 3: Dawn 26.3 2016-08-01 13:24:51 2016-08-03 07:48:52   12.3

【讨论】:

    【解决方案2】:

    请注意,您的 dodf data.frame 正在输入 DO 值作为我建议仅使用 data.frame 并从一开始就将值更改为数字的因素。

    dodf <- data.frame(time = c("2016-05-19 13:00:00", "2016-05-21 15:00:00", 
    "2016-05-26 09:00:00", "2016-05-28 10:00:00", "2016-07-28 09:00:00", "2016-
    07-28 19:00:00", "2016-08-04 04:00:00", "2016-08-01 05:00:00", "2016-08-02 
    16:00:00", "2016-08-05 22:00:00"), 
    DO = c(10.2, 10.4, 10.9, 9.4, 11.9, 12.4, 5.4, 13.2, 12.3, 2.4), 
    Boat = c("Dawn", "Dawn", "Dawn", "Dawn", "Nip", "Nip", "Nip", "Dawn", "Dawn", "Dawn"), 
    Net = c("26", "26", "26", "26", "26", "26", "26", "26.3", "26.3", "26.3"))
    
    library(tidyverse)
    library(lubridate)
    
    dodf %>% 
      left_join(catchdf, by=c('Boat', 'Net')) %>% # join the data.frames
      mutate(time=ymd_hms(time), # assign the values to a data format
             Set = ymd_hms(Set), 
             Lift = ymd_hms(Lift), y = 
               ifelse(time>=Set & time<=Lift,'in', 'out')) %>% # create a "test column"
      filter(y=='in') %>% # filter out values outside of the sample periods
      group_by(Boat, Net) %>% 
      summarise(meanDO = mean(DO))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2017-03-25
      • 2019-08-01
      • 1970-01-01
      • 2022-01-06
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多