我是循环新手。我想做以下事情,但对于整个矢量:
library(timedate)
timeSequence("2014-05-09 10:48:00", "2014-05-11 19:39:00", by = "min")
以下是一个示例:
Dates <- data.frame(Start = c("2014-05-09 10:48:00",
"2014-05-11 19:39:00",
"2014-10-09 08:52:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00"),
End = c("2014-05-11 19:39:00",
"2014-05-12 07:05:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00",
"2014-11-13 08:41:59"))
我开始时:
timeSequence(Dates$Start, Dates$End, by = "min)
但得到错误'from' must be of length 1
...
所以我尝试了for
循环的不同迭代,但没有成功:
for (a in Dates$Start) {
for (b in Dates$End) {
Date_List <- timeSequence(a, b, by = "min")
}
}
我得到Error in seq.int(0, to0 - from, by) : wrong sign in 'by' argument
。我想要一个列表,其中每个项目在向量中的日期之间具有完整序列。非常感谢你。
答案 0 :(得分:1)
您可以使用mapply
library(timeDate)
mapply(timeSequence, from = Dates$Start, to = Dates$End, by = "min", SIMPLIFY = FALSE)
答案 1 :(得分:1)
如果您想使用循环方法,可以尝试以下方法。
require(timeDate)
Dates <- data.frame(Start = c("2014-05-09 10:48:00",
"2014-05-11 19:39:00",
"2014-10-09 08:52:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00"),
End = c("2014-05-11 19:39:00",
"2014-05-12 07:05:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00",
"2014-11-13 08:41:59"))
Dates$Start = as.POSIXct(Dates$Start,format="%Y-%m-%d %H:%M:%S")
Dates$End = as.POSIXct(Dates$End,format="%Y-%m-%d %H:%M:%S")
Date_List=NULL
for (a in 1:nrow(Dates)) {
datetime <- timeSequence(Dates$Start[a],Dates$End[a], by = "mins")
datetime <- datetime[isBizday(datetime, holidayNYSE())]
Date_List[[a]] <- datetime@Data
}