可以按行而不是列来订购输出吗?

时间:2014-01-08 19:51:59

标签: r xts

在下面的代码中我使用apply&为了给出每行MyData中10个元素强度的行的相对等级。有16行,MyData的维度是16x10,垂直日期(xts索引)。然而,我的apply / order语句的输出被反转,日期为列名,16列乘10行。

如何以xts格式创建MyRank,我可以轻松地与价格数据或其他xts元素联系?

由于

MyData = structure(c(0.127, 0.146, 0.175, 0.194, 0.181, 0.164, 0.143, 
            0.141, 0.116, 0.094, 0.095, 0.046, 0.062, 0.051, 0.039, 0.027, 
            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
            0.042, 0.053, 0.083, 0.1, 0.068, 0.037, 0.016, 0.024, 0.004, 
            -0.023, -0.025, -0.056, -0.028, -0.037, -0.059, -0.061, NA, NA, 
            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.159, 
            0.166, 0.164, 0.218, 0.218, 0.182, 0.201, 0.197, 0.176, 0.134, 
            0.164, 0.147, 0.12, 0.07, 0.149, 0.147, 0.113, 0.127, 0.153, 
            0.257, 0.275, 0.183, 0.212, 0.17, 0.171, 0.09, 0.128, 0.064, 
            0.07, 0.06, 0.098, 0.093, 0.235, 0.23, 0.297, 0.302, 0.27, 0.245, 
            0.253, 0.247, 0.215, 0.165, 0.13, 0.086, 0.156, 0.13, 0.103, 
            0.114, 0.247, 0.253, 0.301, 0.387, 0.387, 0.35, 0.302, 0.319, 
            0.282, 0.261, 0.205, 0.151, 0.23, 0.181, 0.167, 0.157, 0.057, 
            0.112, 0.129, 0.1, 0.087, 0.108, 0.115, 0.07, 0.066, 0.025, 0.015, 
            -0.01, 0.02, 0.005, 0.009, 0.009), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts", 
            "zoo"), index = structure(c(1009756800, 1009929600, 1010016000, 
            1010102400, 1010361600, 1010448000, 1010534400, 1010620800, 1010707200, 
            1010966400, 1011052800, 1011139200, 1011225600, 1011312000, 1011657600, 
            1011744000), tzone = "UTC", tclass = "Date"), .Dim = c(16L, 10L
            ), .Dimnames = list(NULL, c("DIA", "DVY", "EEM", "EFA", "EPP", 
            "EWA", "EWC", "EWG", "EWH", "EWI")))

dim(MyData)
is.xts(MyData)

MyRank = apply(MyData, 1, order, decreasing=TRUE)

dim(MyRank)
is.xts(MyRank)

colnames(MyRank) == index(MyData)

1 个答案:

答案 0 :(得分:1)

使用apply.dailyperiod.apply以及您想要使用的周期。

> apply.daily(MyData, order, decreasing=TRUE)
           DIA DVY EEM EFA EPP EWA EWC EWG EWH EWI
2001-12-31   9   8   6   1   7  10   4   2   3   5
2002-01-02   9   8   6   1   7  10   4   2   3   5
2002-01-03   9   8   1   6   7  10   4   2   3   5
2002-01-04   9   8   7   6   1   4  10   2   3   5
2002-01-07   9   7   8   6   1  10   4   2   3   5
2002-01-08   9   8   7   6   1  10   4   2   3   5
2002-01-09   9   8   7   6   1  10   4   2   3   5
2002-01-10   9   8   6   7   1  10   4   2   3   5
2002-01-11   9   8   6   7   1  10   4   2   3   5
2002-01-14   9   8   6   1   7  10   4   2   3   5
2002-01-15   9   6   8   7   1  10   4   2   3   5
2002-01-16   9   6   8   7   1  10   4   2   3   5
2002-01-17   9   8   6   7   1  10   4   2   3   5
2002-01-18   9   8   6   7   1  10   4   2   3   5
2002-01-22   9   6   8   7   1  10   4   2   3   5
2002-01-23   9   6   8   7   1  10   4   2   3   5