0

我有以下 xts 对象:

options("digits.secs" = 1)
ex <- structure(c(NA, -63L, NA, NA, NA, NA, NA, 0L, NA, NA, NA, NA, 
NA, 1L, NA, NA, NA, NA), .Dim = c(6L, 3L), .Dimnames = list(NULL, 
    c("V2", "V3", "V4")), index = structure(c(1366088402.46, 
1366088402.46, 1366088402.463, 1366088402.463, 1366088469.697, 
1366088469.697), tzone = "", tclass = c("POSIXct", "POSIXt")),
class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "")
ex <- make.index.unique(ex, drop = TRUE, fromLast = TRUE)

但是,生成的 xts 对象没有唯一索引。我也尝试过使用strptimewith format="...%OS1",它返回NA所有值。上面的代码直观地对我来说没有意义,因为我正在使用输出选项来尝试截断我的日期。

我搜索了其他人如何处理分数时间戳,大多数结果似乎与上述一致。为什么%OS1对我不起作用,并且options确实是处理此问题的正确方法?我希望我的时间索引以指定的增量在内部被截断,我的索引不应该在每次我设置options("digits.secs")为新的东西时改变。

> options("digits.secs"=3)
> head(ex)
                         V2 V3 V4
2013-04-16 00:00:02.460  NA NA NA
2013-04-16 00:00:02.460 -63  0  1
2013-04-16 00:00:02.463  NA NA NA
2013-04-16 00:00:02.463  NA NA NA
2013-04-16 00:01:09.697  NA NA NA
2013-04-16 00:01:09.697  NA NA NA
> ex <- align.time(ex, n = 0.1)
> head(ex)
                       V2 V3 V4
2013-04-16 00:00:02.5  NA NA NA
2013-04-16 00:00:02.5 -63  0  1
2013-04-16 00:00:02.5  NA NA NA
2013-04-16 00:00:02.5  NA NA NA
2013-04-16 00:01:09.7  NA NA NA
2013-04-16 00:01:09.7  NA NA NA
> ex <- make.index.unique(ex, drop = TRUE, fromLast = TRUE)
> head(ex)
                         V2     V3   V4
2013-04-16 00:00:02.5    NA     NA   NA
2013-04-16 00:01:09.7    NA     NA   NA
2013-04-16 00:01:09.7 -65.5  -7500 0.25
2013-04-16 00:01:13.5 -64.0  -7500 0.25
2013-04-16 00:01:15.4 -64.0 -10000 0.20
2013-04-16 00:01:24.9 -64.0 -10000 0.20

如您所见,我的数据长度已减少到大约三分之一,但即使在前几行中,在 00:01:09.7 也有重复的时间索引。

4

2 回答 2

0

尝试像这样修改您的格式:

strptime(Sys.time(), format = "%Y-%m-%d %H:%M:%S")

'format = "%Y/%m/%d %H:%M:%S"' 输出为 NA:

strptime(Sys.time(), format = "%Y/%m/%d %H:%M:%S")
于 2013-05-14T06:00:36.053 回答
0

看起来您实际上想要聚合您的数据,而不仅仅是使索引值唯一。假设您想对每一列求和,这将起作用。如果您不想这样做,只需将另一个功能替换为您想要的每列。

library(xts)
ex <- structure(c(NA, -63L, NA, NA, NA, NA, NA, 0L, NA, NA, NA, NA, 
NA, 1L, NA, NA, NA, NA), .Dim = c(6L, 3L), .Dimnames = list(NULL, 
    c("V2", "V3", "V4")), index = structure(c(1366088402.46, 
1366088402.46, 1366088402.463, 1366088402.463, 1366088469.697, 
1366088469.697), tzone = "", tclass = c("POSIXct", "POSIXt")),
class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "")

options(digits.secs=6)

# endpoints() doesn't support sub-second resolution on Windows,
# so do what it does using only R code.
ep <- c(0,which(diff(.index(ex)%/%0.1%/%1+1) != 0),NROW(ex))
x <- period.apply(ex, ep, colSums, na.rm=TRUE)
x <- align.time(x, 0.1)
于 2013-05-14T20:04:59.723 回答