如果我理解正確的話,這會做你想要什麼:
## example data
set.seed(1)
judge <- data.frame(judge1 = sample(1:10), judge2 = sample(1:10),
judge3 = sample(1:10), judge4 = sample(1:10))
我們計算的行列使用sapply()
返回行列矩陣,其每一法官。然後我們在這個矩陣的行上使用apply rank()
來計算表演者/行等級。最後的轉置將結果返回到所需的方向。
> t(apply(sapply(judge, rank), 1, rank))
judge1 judge2 judge3
[1,] 1.5 1.5 3.0
[2,] 3.0 1.5 1.5
[3,] 1.0 2.5 2.5
[4,] 2.0 3.0 1.0
[5,] 1.0 2.0 3.0
[6,] 2.5 1.0 2.5
[7,] 3.0 2.0 1.0
[8,] 3.0 1.0 2.0
[9,] 3.0 1.0 2.0
[10,] 1.0 3.0 2.0
裹在一個功能,你是好去:
prank <- function(df, ...) {
t(apply(sapply(df, rank, ...), 1, rank, ...))
}
其中給出:
> prank(judge)
judge1 judge2 judge3
[1,] 1.5 1.5 3.0
[2,] 3.0 1.5 1.5
[3,] 1.0 2.5 2.5
[4,] 2.0 3.0 1.0
[5,] 1.0 2.0 3.0
[6,] 2.5 1.0 2.5
[7,] 3.0 2.0 1.0
[8,] 3.0 1.0 2.0
[9,] 3.0 1.0 2.0
[10,] 1.0 3.0 2.0
的...
允許參數rank()
傳遞中,例如ties.method
參數:
> prank(judge, ties = "min")
judge1 judge2 judge3
[1,] 1 1 3
[2,] 3 1 1
[3,] 1 2 2
[4,] 2 3 1
[5,] 1 2 3
[6,] 2 1 2
[7,] 3 2 1
[8,] 3 1 2
[9,] 3 1 2
[10,] 1 3 2
您是否介意包含玩具數據集以及您期望的輸出? – 2009-09-11 20:07:27
難道你不能只用pmax()在隊伍中? – 2009-09-11 20:26:27
第一個人不能在隊伍中使用pmax(),因爲這會得到最高等級但不是等級等級。我會嘗試拿出一個小例子。 – Farrel 2009-09-12 02:51:36