重叠(相交)时间间隔和 xts [英] overlap(intersect) time interval and xts
问题描述
有两个时间数据集:来自raincollector的数据——时间间隔ti
和start
、end
和rainp
(每期降雨总量,单位为毫米)
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])
和时间序列ts
来自计量站,带有time
和参数q
,即排水量(立方米每秒)>
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])
我需要将时间序列与时间间隔相交,并在 ts
中使用 TRUE/FALSE
创建一个新列,如果该行在下雨间隔 (TRUE代码>),如果不是(
FALSE
),就像这样:
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?
推荐答案
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')
结果:
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
这篇关于重叠(相交)时间间隔和 xts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!