我有两列日期时间。一个是START&其他是END。有没有办法创建一个从START到END的1分钟的时间序列增量,有多行时间间隔......
我有什么......
x <- data.frame(START = c("3/28/2018 9:30", "3/28/2018 9:40"),
END = c("3/28/2018 9:35", "3/28/2018 9:42"))
我想要的......
LOG_START_DT LOG_END_DT
3/28/2018 9:30 3/28/2018 9:35
3/29/2018 9:31 3/29/2018 9:35
3/30/2018 9:32 3/30/2018 9:35
3/31/2018 9:33 3/31/2018 9:35
4/1/2018 9:34 4/1/2018 9:35
4/2/2018 9:35 4/2/2018 9:35
3/28/2018 9:40 3/28/2018 9:42
3/28/2018 9:41 3/28/2018 9:42
3/28/2018 9:42 3/28/2018 9:42
我唯一能想到的就是for循环,但我确信必须有一种更有效的方式。
这就是我所做的很好,但是我的数据有多大需要的时间比我需要的时间长......
out = NULL
for(i in seq_along(x$LOG_START_DT)){
sq = as.POSIXct(seq(from=x$LOG_START_DT[i], to=x$LOG_END_DT[i], by = 60), origin='1970-1-1', tz="UTC")
out = rbindlist(list(out, data.frame(sq)))
}
答案 0 :(得分:0)
使用seq
填充会议记录和rbind
每行或原始数据的数据框架。框架
> x <- data.frame(START = c("2018-03-28 9:30", "2018-03-28 9:40"),
+ END = c("2018-03-28 9:35", "2018-03-28 9:42"))
> rbind(
+ data.frame(LOG_START_DT = seq(as.POSIXct(x[1,1]), as.POSIXct(x[1,2]), by="min"), LOG_END_DT = as.POSIXct(x[1,2])),
+ data.frame(LOG_START_DT = seq(as.POSIXct(x[2,1]), as.POSIXct(x[2,2]), by="min"), LOG_END_DT = as.POSIXct(x[2,2]))
+ )
LOG_START_DT LOG_END_DT
1 2018-03-28 09:30:00 2018-03-28 09:35:00
2 2018-03-28 09:31:00 2018-03-28 09:35:00
3 2018-03-28 09:32:00 2018-03-28 09:35:00
4 2018-03-28 09:33:00 2018-03-28 09:35:00
5 2018-03-28 09:34:00 2018-03-28 09:35:00
6 2018-03-28 09:35:00 2018-03-28 09:35:00
7 2018-03-28 09:40:00 2018-03-28 09:42:00
8 2018-03-28 09:41:00 2018-03-28 09:42:00
9 2018-03-28 09:42:00 2018-03-28 09:42:00
答案 1 :(得分:0)
您可以使用apply
扩展每一行,然后使用Reduce
和rbind
来合并结果:
x <- data.frame(START = c("2018-03-28 9:30", "2018-03-28 9:40", "2018-03-28 9:45"),
END = c("2018-03-28 9:35", "2018-03-28 9:42", "2018-03-28 9:49"))
Reduce(rbind,
apply(x, 1, function(row) {
data.frame(START = seq.POSIXt(from = as.POSIXct(row[1]),
to = as.POSIXct(row[2]),
by = "min"),
END = as.POSIXct(row[2]),
row.names = NULL)
}
)
)
# START END
# 1 2018-03-28 09:30:00 2018-03-28 09:35:00
# 2 2018-03-28 09:31:00 2018-03-28 09:35:00
# 3 2018-03-28 09:32:00 2018-03-28 09:35:00
# 4 2018-03-28 09:33:00 2018-03-28 09:35:00
# 5 2018-03-28 09:34:00 2018-03-28 09:35:00
# 6 2018-03-28 09:35:00 2018-03-28 09:35:00
# 7 2018-03-28 09:40:00 2018-03-28 09:42:00
# 8 2018-03-28 09:41:00 2018-03-28 09:42:00
# 9 2018-03-28 09:42:00 2018-03-28 09:42:00
# 10 2018-03-28 09:45:00 2018-03-28 09:49:00
# 11 2018-03-28 09:46:00 2018-03-28 09:49:00
# 12 2018-03-28 09:47:00 2018-03-28 09:49:00
# 13 2018-03-28 09:48:00 2018-03-28 09:49:00
# 14 2018-03-28 09:49:00 2018-03-28 09:49:00