[英]How to find different time intervals in r
I have a vector of dates that looks like this: 我有一个日期向量,看起来像这样:
"2017-01-05 08:40:00 COT" "2017-01-05 08:50:00 COT" "2017-01-05 09:00:00 COT" "2017-01-05 09:10:00 COT" "2017-01-05 10:30:00 COT" "2017-01-05 10:40:00 COT" "2017-01-05 10:50:00 COT" "2017-01-05 11:00:00 COT" "2017-01-05 11:10:00 COT" "2017-01-05 11:20:00 COT" "2017-01-05 11:30:00 COT" "2017-01-05 11:40:00 COT""2017-01-05 11:50:00 COT" "2017-01-05 12:00:00 COT" "2017-01-05 12:10:00 COT" "2017-01-05 12:20:00 COT" "2017-01-05 12:30:00 COT" "2017-01-05 12:40:00 COT" "2017-01-05 12:50:00 COT" "2017-01-05 13:00:00 COT" "2017-01-05 13:10:00 COT" "2017-01-05 13:20:00 COT" "2017-01-05 13:30:00 COT" "2017-01-05 13:40:00 COT""2017-01-05 13:50:00 COT" "2017-01-05 14:00:00 COT" "2017-01-05 14:10:00 COT" "2017-01-05 14:20:00 COT" "2017-01-05 14:30:00 COT" "2017-01-05 14:40:00 COT" "2017-01-05 14:50:00 COT" "2017-01-05 15:00:00 COT"
and are declare as POSIXct. 并声明为POSIXct。 I need to find the start and end time of the different time intervals in this vector.
我需要在此向量中找到不同时间间隔的开始和结束时间。 For this dates there are 2 time intervals: From
"2017-01-05 08:40:00 COT"
to "2017-01-05 09:10:00 COT"
and from "2017-01-05 10:30:00 COT"
to "2017-01-05 15:00:00 COT"
. 该日期有2个时间间隔:从
"2017-01-05 08:40:00 COT"
到"2017-01-05 09:10:00 COT"
以及从"2017-01-05 10:30:00 COT"
改为"2017-01-05 15:00:00 COT"
。
I have tried the command range(data) but i get the starting date and the end date of the whole vector and not the different intervals. 我已经尝试了命令range(data),但是我得到了整个向量的开始日期和结束日期,而不是不同的间隔。 Hope someone can help
希望有人能帮忙
假设您将所有日期都放在称为“日期”的向量中,则可以执行以下操作
Ranges <- cut(dates, breaks = "2 hours")
If you substitute your text in this input strategy you get a vector of datetimes: 如果将文本替换为该输入策略,则会得到日期时间向量:
dat <- as.POSIXct( scan(text=' ... ',quote="\"",what="") )
# Don't use spaces flanking the ellipsis
Then you could use findInterval to classify the dates and select one with the interval designation you desire, 1 and 3 in your case. 然后,您可以使用findInterval来对日期进行分类,并选择一个您想要的间隔名称(在您的情况下为1和3)。
Breaks <- scan(text='"2017-01-05 08:40:00 COT","2017-01-05 09:10:00 COT","2017-01-05 10:30:00 COT" , "2017-01-05 15:00:00 COT"', sep=",", what="")
# Read 4 items
findInterval(dat, as.POSIXct(Breaks ) )
# [1] 1 1 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4
Using some differences between the times to find the break points in your vector, which I've called tx
(code to create below): As you can see, the results split into 8:40
to 9:10
and then 10:30
to the end. 使用时间之间的一些差异在向量中找到断点,我将其称为
tx
(下面创建的代码):如您所见,结果分为8:40
至9:10
,然后10:30
至结束。
diff(tx)
#Time differences in mins
#[1] 10 10 10 80 10 10 10
cumsum(c(FALSE, diff(tx)!=10))
#[1] 0 0 0 0 1 1 1 1
split(tx, cumsum(c(FALSE, diff(tx)!=10)))
#$`0`
#[1] "2017-01-05 08:40:00 COT" "2017-01-05 08:50:00 COT" "2017-01-05 09:00:00 COT" "2017-01-05 09:10:00 COT"
#
#$`1`
#[1] "2017-01-05 10:30:00 COT" "2017-01-05 10:40:00 COT" "2017-01-05 10:50:00 COT" "2017-01-05 11:00:00 COT"
Where tx
was: tx
在哪里:
tx <- structure(c(1483623600, 1483624200, 1483624800, 1483625400, 1483630200,
1483630800, 1483631400, 1483632000), class = c("POSIXct", "POSIXt"
), tzone = "America/Bogota")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.