r - How to get ranks with no gaps when there are ties among values? -
when there ties in original data, there way create ranking without gaps in ranks (consecutive, integer rank values)? suppose:
x <- c(10, 10, 10, 5, 5, 20, 20) rank(x) # [1] 4.0 4.0 4.0 1.5 1.5 6.5 6.5
in case desired result be:
my_rank(x) [1] 2 2 2 1 1 3 3
i've played options ties.method
option (average
, max
, min
, random
), none of designed provide desired result.
is possible acheive rank()
function?
i can think of quick function this. it's not optimal loop works:)
x=c(1,1,2,3,4,5,8,8) foo <- function(x){ su=sort(unique(x)) (i in 1:length(su)) x[x==su[i]] = return(x) } foo(x) [1] 1 1 2 3 4 5 6 6
Comments
Post a Comment