创建步进时间间隔

时间:2015-04-22 00:55:26

标签: r datetime for-loop sequence

如果我对R相对较新,我很抱歉,但是我已经坚持了一段时间,并且似乎无法通过在线搜索找到我的答案。

我正在尝试根据来自单独数据框(“drop”)的时间创建一列分钟日期时间间隔的数据框。

> head(drops)  
  uid             tid               dtdep              dtpull    nutm     eutm
1 256 A69-1601-27561 2014-04-28 10:14:00 2014-04-28 11:21:00 1966514 329557.4
2 257 A69-1303-54408 2014-04-28 10:22:00 2014-04-28 11:26:00 1966884 329507.1
3 258 A69-1601-27331 2014-04-28 10:28:00 2014-04-28 11:37:00 1967184 329458.8
4 259 A69-1303-31936 2014-04-28 10:38:00 2014-04-28 11:50:00 1968022 329896.8
5 260 A69-1303-54408 2014-04-28 11:32:00 2014-04-28 12:40:00 1966883 329556.9
6 261 A69-1601-27331 2014-04-28 11:45:00 2014-04-28 12:46:00 1967084 329459.0

我创建了一个空数据框来存储我的输出

> steptimes<-data.frame(matrix(NA,nrow=300,ncol=1))

并运行以下for循环,但它只为我的drop的第五行生成时间步骤$ dtdep

>for(j in 1:5){
>  steptimes[j]<-seq(from=drops$dtdep[j],length=60,by="min")
>}

我的输出如下

> steptimes
 [1] "2014-04-28 11:32:00 EDT" "2014-04-28 11:33:00 EDT" "2014-04-28 11:34:00 EDT" "2014-04-28 11:35:00 EDT"
 [5] "2014-04-28 11:36:00 EDT" "2014-04-28 11:37:00 EDT" "2014-04-28 11:38:00 EDT" "2014-04-28 11:39:00 EDT"
 [9] "2014-04-28 11:40:00 EDT" "2014-04-28 11:41:00 EDT" "2014-04-28 11:42:00 EDT" "2014-04-28 11:43:00 EDT"
[13] "2014-04-28 11:44:00 EDT" "2014-04-28 11:45:00 EDT" "2014-04-28 11:46:00 EDT" "2014-04-28 11:47:00 EDT"
[17] "2014-04-28 11:48:00 EDT" "2014-04-28 11:49:00 EDT" "2014-04-28 11:50:00 EDT" "2014-04-28 11:51:00 EDT"
[21] "2014-04-28 11:52:00 EDT" "2014-04-28 11:53:00 EDT" "2014-04-28 11:54:00 EDT" "2014-04-28 11:55:00 EDT"
[25] "2014-04-28 11:56:00 EDT" "2014-04-28 11:57:00 EDT" "2014-04-28 11:58:00 EDT" "2014-04-28 11:59:00 EDT"
[29] "2014-04-28 12:00:00 EDT" "2014-04-28 12:01:00 EDT" "2014-04-28 12:02:00 EDT" "2014-04-28 12:03:00 EDT"
[33] "2014-04-28 12:04:00 EDT" "2014-04-28 12:05:00 EDT" "2014-04-28 12:06:00 EDT" "2014-04-28 12:07:00 EDT"
[37] "2014-04-28 12:08:00 EDT" "2014-04-28 12:09:00 EDT" "2014-04-28 12:10:00 EDT" "2014-04-28 12:11:00 EDT"
[41] "2014-04-28 12:12:00 EDT" "2014-04-28 12:13:00 EDT" "2014-04-28 12:14:00 EDT" "2014-04-28 12:15:00 EDT"
[45] "2014-04-28 12:16:00 EDT" "2014-04-28 12:17:00 EDT" "2014-04-28 12:18:00 EDT" "2014-04-28 12:19:00 EDT"
[49] "2014-04-28 12:20:00 EDT" "2014-04-28 12:21:00 EDT" "2014-04-28 12:22:00 EDT" "2014-04-28 12:23:00 EDT"
[53] "2014-04-28 12:24:00 EDT" "2014-04-28 12:25:00 EDT" "2014-04-28 12:26:00 EDT" "2014-04-28 12:27:00 EDT"
[57] "2014-04-28 12:28:00 EDT" "2014-04-28 12:29:00 EDT" "2014-04-28 12:30:00 EDT" "2014-04-28 12:31:00 EDT"

哪个好,但是我需要为其他滴$ dtdep

生成的步骤时间

如果这非常简单,那么任何帮助都会非常感激和道歉。

谢谢!

1 个答案:

答案 0 :(得分:0)

没有循环可能会更好:

dtdep <- c(as.POSIXct("2014-04-28 10:14:00"), 
          as.POSIXct("2014-04-28 10:22:00"))
drops <- data.frame(dtdep)

steptimes <- lapply(drops$dtdep, function(x){
      seq(from = x, length = 60, by = "min")
})
# As a vector
steptimes <- do.call(c, steptimes)
# As a data frame with one column if this is really necessary
steptimes <- data.frame(steptimes) 

如果您只需要基于第一个 n dtdep元素的时间,请选择steptimes <- lapply(drops$dtdep[1:5], ...

中的那些