overlap(intersect) time interval and xts

自闭症网瘾萝莉.ら 提交于 2020-01-15 09:36:07

问题


There's two time datasets: data from raincollector -- time interval ti with start, end and rain p (total amount of rain per period in mm)

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])

and timeseries ts from gauging station with time and parameter q, which is the water discharge (cu. m per sec)

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])

I need to intersect timeseries with time interval and create a new column in ts with TRUE/FALSE if this row in the rain interval (TRUE) and if it not (FALSE) like this one:

                 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

Have you got any ideas how to apply such simple operation?


回答1:


With 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')

Result:

                 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


来源:https://stackoverflow.com/questions/51880861/overlapintersect-time-interval-and-xts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!