R xts操作产生错误的结果

R xts操作产生错误的结果,r,xts,R,Xts,假设我有三个xts对象a,m,s,用相同的时隙索引,我想计算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','qu

假设我有三个xts对象
a
m
s
,用相同的时隙索引,我想计算
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
列名也相同:

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

现在当我做
n自我回答时:错误在于我认为xts更智能,因为
a/b
考虑了列名,而它没有考虑列名


通过基础矩阵进行除法,而不考虑列名。这就是原因,即使列名集合重合,结果也是错误的。

如果没有正确的解释,很难准确地解释差异。最可能的原因是3个对象的索引值没有像您认为的那样对齐。但如果没有一个可复制的例子,就无法确定。每个对象的
str
输出可能会提供信息。R中的哪些类的行为与您期望xts的方式相同?在执行算术之前,我没有发现任何按行和列对齐的方法。
> setdiff(names(a),names(m))
character(0)
> setdiff(names(m),names(s))
character(0)
> 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
> 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