I am trying to rank order equities (by return for example). As a result I would like to receive a table containing names of stocks in ascending/descending order (parameter to this rank order function) with proper handling of NAs (moved at the end of each row). I really can't figure out an elegant way to do this.
我试图对订单股票进行排名(例如通过回报)。因此,我希望收到一个表格,其中包含按升序/降序排列的股票名称(该等级顺序函数的参数),并适当处理NA(在每行末尾移动)。我真的无法想出一个优雅的方法来做到这一点。
Below is an example of what I want:
以下是我想要的一个例子:
This is coredata of xts object representing some property at different times:
这是xts对象的coredata,表示不同时间的某些属性:
john joe tina jack suzie sasha sven luca
2003-05-29 1 2 3 4 5 6 7 8
2003-06-27 2 3 4 5 6 7 8 1
2003-07-30 3 4 5 6 7 8 1 2
2003-07-31 NA 2 3 4 5 6 1 NA
I need a dataframe which in each row (for each date) displays the column name from previous dataframe of best ranked (based on a property) in column 1, second best in column 2,... Please note that for the last row I need cases with NAs moved to the end (last two columns) or skipped...
我需要一个数据框,每行(对于每个日期)显示第1列中最佳排名(基于属性)的先前数据框中的列名,第2列中第二最佳,...请注意,对于最后一行我需要将NAs移到最后(最后两列)或跳过...
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
2003-05-29 "john" "joe" "tina" "jack" "suzie" "sasha" "sven" "luca"
2003-06-27 "luca" "john" "joe" "tina" "jack" "suzie" "sasha" "sven"
2003-07-30 "sven" "luca" "john" "joe" "tina" "jack" "suzie" "sasha"
2003-07-31 "sven" "joe" "tina" "jack" "suzie" "sasha" "john" "luca"
Thanks in advance for your help. Being a beginner in R this poses to hard of a problem for me...
在此先感谢您的帮助。作为R的初学者,这给我带来了难题......
Kind regards,
Samo.
亲切的问候,萨摩。
1 个解决方案
#1
1
Suppose m
is your original matrix (i.e. the coredata of the xts). You can then get what you want with:
假设m是你的原始矩阵(即xts的coredata)。然后你可以得到你想要的东西:
> nams <- colnames(m)
> t( apply(m, 1, function(r) nams[ order(r) ] ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
2003-05-29 "john" "joe" "tina" "jack" "suzie" "sasha" "sven" "luca"
2003-06-27 "luca" "john" "joe" "tina" "jack" "suzie" "sasha" "sven"
2003-07-30 "sven" "luca" "john" "joe" "tina" "jack" "suzie" "sasha"
2003-07-31 "sven" "joe" "tina" "jack" "suzie" "sasha" "john" "luca"
Note that order(r)
converts a vector r
to a vector p
such that the sequence r[p[1]], r[ p[2] ], r[ p[3] ], ...
is in non-decreasing order, and the default is to push NAs to the end.
注意,阶数(r)将矢量r转换为矢量p,使得序列r [p [1]],r [p [2]],r [p [3]],...处于非递减状态order,默认是将NAs推送到最后。
#1
1
Suppose m
is your original matrix (i.e. the coredata of the xts). You can then get what you want with:
假设m是你的原始矩阵(即xts的coredata)。然后你可以得到你想要的东西:
> nams <- colnames(m)
> t( apply(m, 1, function(r) nams[ order(r) ] ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
2003-05-29 "john" "joe" "tina" "jack" "suzie" "sasha" "sven" "luca"
2003-06-27 "luca" "john" "joe" "tina" "jack" "suzie" "sasha" "sven"
2003-07-30 "sven" "luca" "john" "joe" "tina" "jack" "suzie" "sasha"
2003-07-31 "sven" "joe" "tina" "jack" "suzie" "sasha" "john" "luca"
Note that order(r)
converts a vector r
to a vector p
such that the sequence r[p[1]], r[ p[2] ], r[ p[3] ], ...
is in non-decreasing order, and the default is to push NAs to the end.
注意,阶数(r)将矢量r转换为矢量p,使得序列r [p [1]],r [p [2]],r [p [3]],...处于非递减状态order,默认是将NAs推送到最后。