2013-07-03 45 views
-3

我知道這個問題很類似這樣的:添加均值/平均束縛隊伍行列列

Add a column of ranks

考慮到我們有這樣的數據:

test <- data.frame(A=c("aaabbb", 
"aaaabb", 
"aaaabb", 
"aaaaab", 
"bbbaaa", 
"bbbbaa"), 
B=c("10.00", 
"00.04", 
"00.04", 
"00.00", 
"20.00", 
"00.06" 
)) 

我需要儘管如此,我還是有這樣的東西:

> test 
    A  B  C 
1 aaabbb 10.00 1 
2 aaaabb 00.04 2.5 
3 aaaabb 00.04 2.5 
4 aaaaab 00.00 3 
5 bbbaaa 20.00 4 
6 bbbbaa 00.06 5 

編輯:

> dput(qual_orderedadj_ranks) 

structure(list(words = structure(c(29L, 7L, 28L, 6L, 19L, 21L, 
9L, 11L, 30L, 1L, 8L, 10L, 13L, 12L, 5L, 26L, 27L, 32L, 33L, 
3L, 22L, 18L, 16L, 24L, 25L, 31L, 23L, 2L, 17L), .Label = c("average","yellow", "emerald", 
"sense","slate", "turcquoise", "green", "orange", "fair", "chestnut", "sand", "good", 
"silver", "sense", "sense", "gray", "lousy", "wine", "smalt", "sense", "taupe", "poor", 
"blue", "red", "black", "gold", "white", "teal", "terracotta", "purple", "violett", 
"olive", "khaki"), class = "factor"), enzo = c(9.57973168019844, 2.68331227860491, 
1.85920971038049, 1.28384868054554, 0.885031778228944, 0.740942048756444, 
0.415649187810432, 0.0418303446590026, 0.0836608598897025, 0.680367202534345, 
1.53377945661345, 1.70660459871111, 39.2413924890553, 
239.081124461913, 0, 0, 0, 0, 0, 86.5734538416169, 24.2262630473592, 
0.669305983927372, 0.5093534157301, 0.25098462655732, 0.0836608598897025, 
0.0418303446590026, 0.276963945905033, 0.839118699701029, 1.00634089909635), 
ranks = c(1, 2, 3, 4, 5, 6, 7, 17, 17, 10, 11, 12, 13, 14, 
17, 17, 17, 17, 17, 20, 21, 22, 23, 24, 17, 17, 27, 28, 29)), row.names = 
c(1L, 2L, 3L, 4L, 6L, 8L, 9L, 28L, 27L, 22L, 21L, 20L, 18L, 16L, 11L, 
12L, 13L, 14L, 15L, 17L, 19L, 23L, 24L, 25L, 26L, 29L, 10L, 7L, 
5L), .Names = c("words", "enzo", "ranks"), class = "data.frame") 
+0

有你甚至試圖功能'rank' ? (您的輸出與您的示例數據不匹配。) – joran

回答

1

試試這個:

within(test, B <- rank(A)) 

或者,如果你想使用A原來的順序:

within(test, B <- ave(seq_along(A), by=A)) 
+0

...或者只是'測試$ B < - 等級(測試$ A)'? – joran

+0

@joran,肯定,感謝:-) –

+0

@ Ferdinand.kraft感謝,這是你的第二個建議,我確實想保持原來的順序。然而,我有一種感覺,這種方法可能不夠用細粒度我需要:像0 - 0.04 - 0.08和0.06這樣的值被賦予相同的並列等級。 有沒有辦法進一步區分這種差異? – jaspb