基于R中表B的秩排序表A.

时间:2022-08-26 12:37:58

pretty newb question here, but I have not been able to track down a solution for some time:

这里有一个很新的问题,但我有一段时间没能找到解决方案:

I have an XTS object of trading indicators (indicate) for stock data that looks like

我有一个XTS对象的交易指标(指示)看起来像股票数据

               A           XOM          MSFT
2000-11-30   -0.59        0.22          0.10
2000-12-29    0.55       -0.23          0.05
2001-01-30   -0.52        0.09         -0.10

And a table with an identical index for the corresponding period returns (return) that looks like

并且具有相同句点的相同索引的表返回(返回)看起来像

               A           XOM          MSFT
2000-11-30   -0.15        0.10          0.03
2000-12-29    0.03       -0.05          0.02
2001-01-30   -0.04        0.02         -0.05

I have sorted the indicator table and had it return the column name with the following code:

我已经对指标表进行了排序,并使用以下代码返回列名:

indicate.label <- colnames(indicate)
indicate.rank <- t(apply(indicate, 1, function(x) indicate.label[order(-x)]))    
indicate.rank <- xts(indicate.rank, order.by = index(returns))

Which gives the table (indicate.rank) of the symbol names ranked by their trading indicator:

这给出了按交易指标排名的符号名称表(indication.rank):

               1            2            3
2000-11-30    XOM         MSFT           A
2000-12-29     A          MSFT          XOM
2001-01-30    XOM          A            MSFT

I would like to also have a table that gives the period returns based on the indicator rank:

我想有一个表格,根据指标排名给出期间回报:

2000-11-30    0.10         0.03         -0.15
2000-12-29    0.03         0.02         -0.05
2001-01-30    0.02        -0.04         -0.05

I cannot figure out how to call the correct symbol for all rows or just sort the table return based on the order of indicate.

我无法弄清楚如何为所有行调用正确的符号,或者只是根据指示的顺序对表返回进行排序。

Thank you for any suggestions.

谢谢你的任何建议。

Trevor J

1 个解决方案

#1


1  

I'm not particularly satisfied with this solution, but it works.

我对这个解决方案并不是特别满意,但它确实有效。

row.rank <- t(apply(indicate, 1, order, decreasing=TRUE))
indicate.rank <- return.rank <- indicate  # pre-allocate
for(i in 1:NROW(indicate.rank)) {
  indicate.rank[i,] <- colnames(indicate)[row.rank[i,]]
  return.rank[i,] <- return[i,row.rank[i,]]
}

It would probably be easier to handle this if the returns and the indicators for each symbol were in the same object, but I don't know how that would fit with the rest of your workflow.

如果每个符号的返回值和指示符在同一个对象中,则可能更容易处理此问题,但我不知道这将如何适合您的工作流程的其余部分。

#1


1  

I'm not particularly satisfied with this solution, but it works.

我对这个解决方案并不是特别满意,但它确实有效。

row.rank <- t(apply(indicate, 1, order, decreasing=TRUE))
indicate.rank <- return.rank <- indicate  # pre-allocate
for(i in 1:NROW(indicate.rank)) {
  indicate.rank[i,] <- colnames(indicate)[row.rank[i,]]
  return.rank[i,] <- return[i,row.rank[i,]]
}

It would probably be easier to handle this if the returns and the indicators for each symbol were in the same object, but I don't know how that would fit with the rest of your workflow.

如果每个符号的返回值和指示符在同一个对象中,则可能更容易处理此问题,但我不知道这将如何适合您的工作流程的其余部分。