R:秩函数有两个变量和ties.method随机

时间:2016-12-19 00:43:04

标签: r ranking rank

在R中是否有办法使用具有多个条件和tie.method的等级函数(或类似的东西)?

通常,排名用于对向量中的值进行排名,如果存在关联,则可以使用其中一种关系方法("平均","随机","第一& #34;,...)。但是当在矩阵中对列进行排名时,我想使用多个列之一的tie方法。

一个最小的例子:

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" - 参数。

换句话说,可能的结果可能是:

       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 - 函数(即m[order(m[,2], m[,3], sample(length(x))),],但我希望得到排名值,而不是排序矩阵的索引。

如果您需要详细说明我为什么需要排名值,请随时提出,我将使用额外的详细信息编辑问题。现在我想最小的例子就可以了。

编辑:@alistaire指出,将数据框更改为矩阵。

3 个答案:

答案 0 :(得分:2)

由于order(order(x))提供与rank(x)相同的结果(请参阅Why does order(order(x)) equal rank(x) in R?),您可以这样做

order(order(y, z, runif(length(y))))

获得排名值。

这是一种更为复杂的方法,允许您使用ties.method中的方法。它需要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

答案 1 :(得分:1)

抱歉,我最初误解了你的问题。我认为这就是你想要的。我做了一个小改动。具体来说,我将变量df设为数据帧,而不仅仅是矩阵。

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)
df <- data.frame(x=x,y=y, z=z)

TM = "last"     ## Your desired ties method here.
df[rank(df$z, ties.method=TM),] = df
df = df[order(df$y),]
df
    x y   z
4   1 1 0.2
9   8 1 0.7
3   5 2 0.2
5  10 3 0.3
6   9 3 0.3
10  2 4 0.8
7   4 5 0.4
8   3 5 0.5
1   7 8 0.1
2   6 8 0.1

您可以使用排名中可用的任何关联方法,但我选择使用&#34; last&#34;这是为了强调它使订单切换。

答案 2 :(得分:0)

使用model = Sequential() model.add(Bidirectional(LSTM(hidden_size, recurrent_activation='sigmoid', return_sequences=True), input_shape=(None, input_size), name='rnn')) bias_size = rnn1.weight_hh_l0.detach().cpu().numpy().T.shape[1] keras_format_weights = [ rnn1.weight_ih_l0.detach().cpu().numpy().T, rnn1.weight_hh_l0.detach().cpu().numpy().T, np.zeros((bias_size,)), rnn1.weight_ih_l0_reverse.detach().cpu().numpy().T, rnn1.weight_hh_l0_reverse.detach().cpu().numpy().T, np.zeros((bias_size,)) ] 的{​​{1}}函数怎么样?

data.table
相关问题