Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的日期向量提取区间_R_Date - Fatal编程技术网

从R中的日期向量提取区间

从R中的日期向量提取区间,r,date,R,Date,我有一个简单的日期向量: > head(as.vector(times)) [1] "2015.08.04 10:00:00.790395" "2015.08.04 10:00:00.884402" "2015.08.04 10:00:01.015408" "2015.08.04 10:00:01.016410" [5] "2015.08.04 10:00:01.017410" "2015.08.04 10:00:01.370429" Vector真的很大:~500

我有一个简单的日期向量:

> head(as.vector(times))
  [1] "2015.08.04 10:00:00.790395" "2015.08.04 10:00:00.884402" 
      "2015.08.04 10:00:01.015408" "2015.08.04 10:00:01.016410"
  [5] "2015.08.04 10:00:01.017410" "2015.08.04 10:00:01.370429"
Vector真的很大:~500万件物品。 我想从这些数据中提取5分钟间隔。让我们看看算法:

t0 <- strptime("2015.08.04 10:00:00.000000", format = "%Y.%m.%d %H:%M:%OS")
t1 <- strptime("2015.08.04 10:05:00.000000", format = "%Y.%m.%d %H:%M:%OS")
times <- strptime(times, format = "%Y.%m.%d %H:%M:%OS")
# indexes of last dates in each interval
lastIntervalIndexes <- c()
counter <- 1
while (t1 < times[length(times)]) {
   dates <- which(times >= t0 & times < t1)
   if (length(dates) > 0) {
      lastIntervalIndexes[counter] <- last(dates)
      counter <- counter + 1
   }
   t0 <- t1
   t1 <- t1 + 5 * 60
}
这是正确的,但确实很长。如何以最快的方式进行此操作

感谢您的关注。

您可以使用LibraryUBridate,并根据您的需要安装以下示例:

library(lubridate)

times <- c("2015.08.04 10:00:00.790395", "2015.08.04 10:00:00.884402",
           "2015.08.04 10:04:01.015408", "2015.08.04 10:05:01.016410",
           "2015.08.04 10:06:01.017410", "2015.08.04 10:10:01.370429")

interval <- interval(start = ymd_hms("2015.08.04 10:00:00.000000"),
                       end = ymd_hms("2015.08.04 10:05:00.000000"))
times <- ymd_hms(times)
inside <- times %within% interval
times[inside]

# "2015-08-04 10:00:00 UTC" "2015-08-04 10:00:00 UTC" "2015-08-04 10:04:01 UTC"
请注意,我已经多次更改了您给定的一些时间