I'm new to R, and i find it quite interesting.
I have MATLAB code to rank a vector based on order which works fine. Now I want to convert it to R code, a typical spearman ranking with ties:
# MATLAB CODE
function r=drank(x)
u = unique(x);
[xs,z1] = sort(x);
[z1,z2] = sort(z1);
r = (1:length(x))';
r=r(z2);
for i=1:length(u)
s=find(u(i)==x);
r(s,1) = mean(r(s));
end
This is what i tried:
# R CODE
x = c(10.5, 8.2, 11.3, 9.1, 13.0, 11.3, 8.2, 10.1)
drank <- function(x){
u = unique(x)
xs = order(x)
r=r[xs]
for(i in 1:length(u)){
s=which(u[i]==x)
r[i] = mean(r[s])
}
return(r)
}
r <- drank(x)
Results:
r = 5, 1.5, 6.5, 3, 8, 6.5, 1.5, 4
1.5 is average of 8.2 occurring twice ie. tie
6.5 is average of 11.3 occurring twice
Can anyone help me check it?
Thanks,
R has a built-in function for ranking, called
rank()and it gives precisely what you are looking for.rankhas the argumentties.method, "a character string specifying how ties are treated", which defaults to"average", i.e. replaces ties by their mean.