重叠(相交)时间间隔和 xts
overlap(intersect) time interval and xts
有两个时间数据集:来自 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])
我需要将时间序列与时间间隔相交,并在 ts
中用 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
你知道如何应用这样简单的操作吗?
与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
有两个时间数据集:来自 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])
我需要将时间序列与时间间隔相交,并在 ts
中用 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
你知道如何应用这样简单的操作吗?
与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