简体   繁体   English

将向量中的每个值替换为data.frame的等级编号

[英]Replacing each value in a vector with its rank number for a data.frame

In this hypothetical scenario, I have performed 5 different analyses on 13 chemicals, resulting in a score assigned to each chemical within each analysis. 在这个假设情景中,我对13种化学物质进行了5次不同的分析,得出每次分析中每种化学物质的分数。 I have created a table as follows: 我创建了一个表如下:

---- Analysis1 Analysis2 Analysis3 Analysis4 Analysis5 Chem_1 3.524797844 4.477695034 4.524797844 4.524797844 4.096698498 Chem_2 2.827511555 3.827511555 3.248136118 3.827511555 3.234398548 Chem_3 2.682144761 3.474646298 3.017780505 3.682144761 3.236152242 Chem_4 2.134137304 2.596921333 2.95181339 2.649076603 2.472875191 Chem_5 2.367736454 3.027814219 2.743137896 3.271122346 2.796607809 Chem_6 2.293110565 2.917318708 2.724156207 3.293110565 2.530967343 Chem_7 2.475709113 3.105794018 2.708222528 3.475709113 3.088819908 Chem_8 2.013451822 2.259454085 2.683273938 2.723554966 2.400976121 Chem_9 2.345123123 3.050074893 2.682845391 3.291851228 2.700844104 Chem_10 2.327658894 2.848729452 2.580415233 3.327658894 2.881490893 Chem_11 2.411243882 2.98131398 2.554456095 3.411243882 3.109205453 Chem_12 2.340778276 2.576860244 2.549707035 3.340778276 3.236545826 Chem_13 2.394698249 2.90682524 2.542599327 3.394698249 3.12936843

I would like to create columns corresponding to each analysis which contain the rank position for each chemical. 我想创建对应于每个分析的列,其中包含每种化学品的等级位置。 For instance, under Analysis1 , Chem_1 would have value "1", Chem_2 would have value "2", Chem_3 would have value "4", Chem_7 would have value "4", Chem_11 would have value "5", and so on. 例如,在Analysis1下, Chem_1值为“1”, Chem_2值为“2”, Chem_3值为“4”, Chem_7值为“4”, Chem_11值为“5”,依此类推。

We can use dense_rank from dplyr 我们可以使用dense_rankdplyr

library(dplyr)
df %>%
     mutate_each(funs(dense_rank(-.))) 

In base R , we can do base R ,我们可以做到

df[] <- lapply(-df, rank, ties.method="min")

In data.table , we can use data.table ,我们可以使用

library(data.table)
setDT(df)[, lapply(-.SD, frank, ties.method="dense")]

To avoid the copies from multiplying with - , as @Arun mentioned in the comments 避免副本与-相乘,如@Arun在评论中提到的那样

lapply(.SD, frankv, order=-1L, ties.method="dense")

You can also do this in base R: 您也可以在R基础上执行此操作:

cbind("..." = df[,1], data.frame(do.call(cbind, 
                                         lapply(df[,-1], order, decreasing = T))))

       ... Analysis1 Analysis2 Analysis3 Analysis4 Analysis5
1   Chem_1         1         1         1         1         1
2   Chem_2         2         2         2         2        12
3   Chem_3         3         3         3         3         3
4   Chem_4         7         7         4         7         2
5   Chem_5        11         9         5        11        13
6   Chem_6        13         5         6        13        11
7   Chem_7         5        11         7        12         7
8   Chem_8         9         6         8        10        10
9   Chem_9        12        13         9         6         5
10 Chem_10        10        10        10         9         9
11 Chem_11         6         4        11         5         6
12 Chem_12         4        12        12         8         4
13 Chem_13         8         8        13         4         8

If I'm not mistaken, you want to have the column-wise rank of your table. 如果我没有弄错的话,你想拥有你的表的列级排名。 Here is my solution: 这是我的解决方案:

m=data.matrix(df) # converts data frame to matrix, convert your data to matrix accordingly
apply(m, 2, function(c) rank(c)) # increasingly
apply(m, 2, function(c) rank(-c)) # decreasingly

However, I believe you could solve it by yourself with the help of the answers to this question Get rank of matrix entries? 但是,我相信你可以借助这个问题的答案自己解决它获得矩阵条目的排名?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM