1

In the following code I use apply & order to give a relative rank, by row, of the strength of the 10 elements in each row of MyData. There are 16 rows and the dimension of MyData is 16x10 with dates (the xts index) vertically. The output of my apply/order statement is however reversed with the dates as columns names and 16 columns by 10 rows.

How can I get MyRank created in an xts format which I'd be able to easily cbind with price data or other xts elements?

Thanks

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)
LGTrader
  • 2,349
  • 4
  • 23
  • 29

1 Answers1

1

Use apply.daily, or period.apply with whatever periodicity you want to use.

> 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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418