a <- c("12/4/2010 9:30:00","12/4/2010 9:31:00","12/4/2010 9:32:00",
"12/4/2010 9:33:00","12/4/2010 9:34:00","12/4/2010 9:35:00")
b <- strptime(a,"%d/%m/%Y %H:%M:%S")
> time(a)
[1] 1 2 3 4 5 6
attr(,"tsp")
[1] 1 6 1
> time(b)
Error in hasTsp(x) : invalid time series parameters specified
> timeBased(a)
[1] FALSE
> timeBased(b)
[1] TRUE
有没有办法让它适用于time
和timeBased
个函数,这意味着在使用time
并使用TRUE
返回timeBased
时没有错误?
答案 0 :(得分:1)
time
没有按照您的想法行事。它是通用的,所以它根据你给它的对象类型做不同的事情,但一般来说,它给你索引,它与index
> require("xts")
> x <- xts(1:6, b)
> time(x)
[1] "2010-04-12 09:30:00 CDT" "2010-04-12 09:31:00 CDT"
[3] "2010-04-12 09:32:00 CDT" "2010-04-12 09:33:00 CDT"
[5] "2010-04-12 09:34:00 CDT" "2010-04-12 09:35:00 CDT"
> index(x)
[1] "2010-04-12 09:30:00 CDT" "2010-04-12 09:31:00 CDT"
[3] "2010-04-12 09:32:00 CDT" "2010-04-12 09:33:00 CDT"
[5] "2010-04-12 09:34:00 CDT" "2010-04-12 09:35:00 CDT"
> timeBased(x)
[1] FALSE
> timeBased(index(x))
[1] TRUE
> timeBased(time(x))
[1] TRUE