我在一个xts R对象中有三个向量,称为V1、V2、V3。合并后,它们从左到右的顺序是V2、V3、V1。如何重新排列它们,使它们从左到右读为V1、V2、V3?
x <- x[,c(2,3,1)]
> data(sample_matrix)
> x <- head(as.xts(sample_matrix, descr='my new xts object'))[,c(1,2,3)]
> x
Open High Low
2007-01-02 50.03978 50.11778 49.95041
2007-01-03 50.23050 50.42188 50.23050
2007-01-04 50.42096 50.42096 50.26414
2007-01-05 50.37347 50.37347 50.22103
2007-01-06 50.24433 50.24433 50.11121
2007-01-07 50.13211 50.21561 49.99185
> x <- x[,c(2,3,1)]
> x
High Low Open
2007-01-02 50.11778 49.95041 50.03978
2007-01-03 50.42188 50.23050 50.23050
2007-01-04 50.42096 50.26414 50.42096
2007-01-05 50.37347 50.22103 50.37347
2007-01-06 50.24433 50.11121 50.24433
2007-01-07 50.21561 49.99185 50.13211
xts.object$V1plusV2 <- xts.object[, "V1"] + xts.object[, V2"] ,
您无法通过名称重新排序列。以下操作是不允许的
xts.reordered <- xts.object[, c("V3, V2, V1")] .
正如Shane所指出的那样,您需要按列号引用列。