【问题标题】:overlap(intersect) time interval and xts重叠(相交)时间间隔和 xts
【发布时间】:2018-08-16 15:44:10
【问题描述】:

有两个时间数据集:来自raincollector 的数据——时间间隔ti 与start、end 和雨p(每个周期的总降雨量,以毫米为单位)

ti <- data.frame(
             start = c("2017-06-05 19:30:00", "2017-06-06 12:00:00"),
               end = c("2017-06-05 23:30:00", "2017-06-06 14:00:00"),
                 p = c(16.4, 4.4)
      )

ti[,1] <- as.POSIXct(ti[, 1])
ti[,2] <- as.POSIXct(ti[, 2])

时间序列ts 来自计量站,time 和参数q,即排水量(立方米/秒)

ts <- data.frame(stringsAsFactors=FALSE,
              time = c("2017-06-05 16:00:00", "2017-06-05 19:00:00",
                       "2017-06-05 21:00:00", "2017-06-05 23:00:00",
                       "2017-06-06 9:00:00", "2017-06-06 11:00:00", "2017-06-06 13:00:00",
                       "2017-06-06 16:00:00", "2017-06-06 17:00:00"),
                 q = c(0.78, 0.84, 0.9, 0.78, 0.78, 0.78, 0.78, 1.22, 1.25)
      )
ts[,1] <- as.POSIXct(ts[,1])

我需要将时间序列与时间间隔相交并在TRUE/FALSE 中创建一个新列,如果此行在雨间隔(TRUE)中,如果不是(FALSE)像这样:

                 time    q  rain
1 2017-06-05 16:00:00 0.78 FALSE
2 2017-06-05 19:00:00 0.84 FALSE
3 2017-06-05 21:00:00 0.90  TRUE # there were rain
4 2017-06-05 23:00:00 0.78  TRUE # there were rain
5  2017-06-06 9:00:00 0.78 FALSE
6 2017-06-06 11:00:00 0.78 FALSE
7 2017-06-06 13:00:00 0.78  TRUE # there were rain
8 2017-06-06 16:00:00 1.22 FALSE
9 2017-06-06 17:00:00 1.25 FALSE

你有什么想法如何应用这种简单的操作吗?

【问题讨论】:

  • p 和 q 的目的是什么?
  • @user 他们都是我正在调查的变量。 p 是从start 到end 期间的液体沉淀总量(以毫米为单位)。而q是河流的排水量

标签: r xts overlap intersect


【解决方案1】:

与sqldf:

library(sqldf)
sqldf('select ts.*, case when ti.p is not null then 1 else 0 end as rain 
      from ts
      left join ti
      on start <= time and
         time <= end')

结果:

                 time    q rain
1 2017-06-05 16:00:00 0.78    0
2 2017-06-05 19:00:00 0.84    0
3 2017-06-05 21:00:00 0.90    1
4 2017-06-05 23:00:00 0.78    1
5  2017-06-06 9:00:00 0.78    0
6 2017-06-06 11:00:00 0.78    0
7 2017-06-06 13:00:00 0.78    1
8 2017-06-06 16:00:00 1.22    0
9 2017-06-06 17:00:00 1.25    0

【讨论】:

  • 我们可以写... on time between strt and end
  • @G.Grothendieck 我们可以,但我通常不愿意使用between 而不是&lt;= 运算符,因为between 是否包含在内可能并不明显。
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多