R:具有两个变量和 ties.method random 的秩函数 [英] R: Rank-function with two variables and ties.method random
问题描述
在 R 中有没有办法在多个条件和 ties.method 中使用排名函数(或类似的东西)?
Is there a way in R to use the rank function (or something similar) with multiple criteria and a ties.method?
通常排名用于对向量中的值进行排名,如果存在联系,您可以使用联系方法之一(平均"、随机"、第一"等).但是在对矩阵中的一列进行排名时,我想使用多列和其中一种联系方法.
Normally rank is used to rank values in a vector and if there are ties you can use one of the ties methods ("average", "random", "first", ...). But when ranking a column in a matrix, I would like to use multiple columns and one of the ties methods.
一个最小的例子:
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(1, 4, 5, 5, 2, 8 ,8, 1,3, 3)
z <- c(0.2, 0.8, 0.5, 0.4, 0.2, 0.1, 0.1, 0.7, 0.3, 0.3)
m <- cbind(x=x,y=y, z=z)
想象一下,我想对上述矩阵中的 y
值进行排名.但是如果有关系,我希望函数查看 z
值.如果在那之后仍然有关系,那么我想使用 ties.method = "random"
参数.
Imagine I want to rank the y
-values in the above matrix. But if there are ties, I want the function to look at the z
-values. If there still are ties after that, then I want to use the ties.method = "random"
-parameter.
换句话说,可能的结果是:
In other words, a possible outcome could be:
x y z
[1,] 1 1 0.2
[2,] 8 1 0.7
[3,] 5 2 0.2
[4,] 9 3 0.3
[5,] 10 3 0.3
[6,] 2 4 0.8
[7,] 4 5 0.4
[8,] 3 5 0.5
[9,] 6 8 0.1
[10,] 7 8 0.1
但也可能是这样:
x y z
[1,] 1 1 0.2
[2,] 8 1 0.7
[3,] 5 2 0.2
[4,] 10 3 0.3
[5,] 9 3 0.3
[6,] 2 4 0.8
[7,] 4 5 0.4
[8,] 3 5 0.5
[9,] 7 8 0.1
[10,] 6 8 0.1
注意第四行和第五行的不同(就像第九行和第十行一样).上述结果我已经能够用 order
-function 得到(即 m[order(m[,2], m[,3], sample(length(x))),]
,但我想接收秩值,而不是排序矩阵的索引.
Notice how the fourth and the fifth row are different (just as the ninth and the tenth). The above outcome I've been able to get with the order
-function (i.e. m[order(m[,2], m[,3], sample(length(x))),]
, but I'd like to receive the rank-values, not the indices of a sorted matrix.
如果您需要详细说明为什么我需要排名值,请随时提问,我会用额外的细节编辑问题.现在我认为最小的例子就可以了.
If you need elaboration on why I need the rank-values, feel free to ask and I'll edit the question with extra details. For now I think the minimal example will do.
如@alistaire 指出的那样,将数据帧更改为矩阵.
Changed dataframe to matrix as @alistaire pointed out.
推荐答案
由于 order(order(x))
给出与 rank(x)
相同的结果(请参阅为什么在 R 中 order(order(x)) 等于 rank(x)?),你可以这样做
Since order(order(x))
gives the same result as rank(x)
(see Why does order(order(x)) equal rank(x) in R?), you could just do
order(order(y, z, runif(length(y))))
获取排名值.
这是一种更复杂的方法,它允许您使用 ties.method
中的方法.它需要 dplyr
:
Here's a more involved approach that allows you to use methods from ties.method
. It requires dplyr
:
library(dplyr)
rank2 <- function(df, key1, key2, ties.method) {
average <- function(x) mean(x)
random <- function(x) sample(x, length(x))
df$r <- order(order(df[[key1]], df[[key2]]))
group_by_(df, key1, key2) %>% mutate(rr = get(ties.method)(r))
}
rank2(df, "y", "z", "average")
# Source: local data frame [10 x 5]
# Groups: y, z [8]
# x y z r rr
# <dbl> <dbl> <dbl> <int> <dbl>
# 1 1 1 0.2 1 1.0
# 2 2 4 0.8 6 6.0
# 3 3 5 0.5 8 8.0
# 4 4 5 0.4 7 7.0
# 5 5 2 0.2 3 3.0
# 6 6 8 0.1 9 9.5
# 7 7 8 0.1 10 9.5
# 8 8 1 0.7 2 2.0
# 9 9 3 0.3 4 4.5
# 10 10 3 0.3 5 4.5
这篇关于R:具有两个变量和 ties.method random 的秩函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!