xts操作产生错误的结果

时间:2017-03-01 23:39:45

标签: r xts

假设我有三个xts对象ams,使用相同的时间段编制索引,我想计算abs((a*20)-m)/s。这适用于以下简单情况:

bla <- data.frame(c("2016-09-03 13:00", "2016-09-03 13:10", "2016-09-03 13:20"),c(1,2,3), c(4,5,6), c(7,8,9))
names(bla) <- c('ts','lin','qua','cub')
a <- as.xts(x = bla[,c('lin','qua','cub')], order.by=as.POSIXct(bla$ts)
... similar for m and s...
abs((a*20)-m)/s

给出正确的结果。

当我查看我的真实数据时,我看到了不同的行为:

> class(a)
[1] "xts" "zoo"
> class(m)
[1] "xts" "zoo"
> class(s)
[1] "xts" "zoo"
> dim(a)
[1]    1 4650
> dim(m)
[1]    1 4650
> dim(s)
[1]    1 4650

列名也是相同的:

> setdiff(names(a),names(m))
character(0)
> setdiff(names(m),names(s))
character(0)

现在当我n <- abs((a*20)-m)/s时,我得到了

> n[1,feature]
                     feature
2016-09-08 14:00:00  12687075516

但如果我手工计算:

> aa <- coredata((a*20)[1,feature])[1,1]
> mm <- coredata(m[1,feature])[1,1]
> ss <- coredata(s[1,feature])[1,1]
> abs(aa-mm)/ss
     feature 
0.0005893713 

只是给出原始值:

> a[1,feature]
                        feature
2016-09-08 14:00:00 27955015680
> m[1,feature]
                         feature
2016-09-08 14:00:00 559150430034
> s[1,feature]
                        feature
2016-09-08 14:00:00 85033719103

任何人都可以解释这种差异吗?

非常感谢

诺贝特

1 个答案:

答案 0 :(得分:0)

自我回答:错误是我认为xts在a/b考虑列名称的意义上更加智能,而

> a
                    lin qua cub
2016-09-03 13:00:00   1   4   7
2016-09-03 13:10:00   2   5   8
2016-09-03 13:20:00   3   6   9
> b
                    qua lin cub
2016-09-03 13:00:00   2   3   4
2016-09-03 13:10:00   2   3   4
2016-09-03 13:20:00   2   3   4
> a/b
                    lin      qua  cub
2016-09-03 13:00:00 0.5 1.333333 1.75
2016-09-03 13:10:00 1.0 1.666667 2.00
2016-09-03 13:20:00 1.5 2.000000 2.25

除法通过基础矩阵完成,而不考虑列名。这就是为什么即使列名称集重合,结果也是错误的。

相关问题