finance - Ordering Table A based on Rank of Table B in R -
pretty newb question here, have not been able track down solution time:
i have xts object of trading indicators (indicate) stock data looks like
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 table identical index corresponding period returns (return) looks like
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 indicator table , had return column name 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 table (indicate.rank) of symbol names ranked trading indicator:
1 2 3 2000-11-30 xom msft 2000-12-29 msft xom 2001-01-30 xom msft
i have table gives period returns based on 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 call correct symbol rows or sort table return based on order of indicate.
thank suggestions.
trevor j
i'm not particularly satisfied solution, 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 easier handle if returns , indicators each symbol in same object, don't know how fit rest of workflow.
Comments
Post a Comment