# Rank correlation not equal correlation on ranks in cor()

Why am I getting different results for Pearson and Spearman correlation, although I am already comparing ranks?

a <- c(1,2,3,1,2,3)
b <- c(1,2,2,1,2,3)
cor(a, b, method="pearson")
# [1] 0.8911328
cor(a, b, method="spearman")
# [1] 0.9036961


Formula to use when there are tied ranks seems identical to formula for pearson correlation.

As far as I understand, spearman correlation is just pearson correlation on the ranks. Right?

R function documentation

• your inputs are not ranks in the usual meaning of the term? – mdewey Jun 4 '18 at 14:57
• What would be the corresponding ranks if they are not? – PalimPalim Jun 4 '18 at 14:57
• Try cor(rank(a), rank(b), method = ...) – mdewey Jun 4 '18 at 15:02
• Consider the data in a as if it represented positions in a race among six people: two people tied for each position. There are various ways to assign ranks with ties, but typically the ranks that would be given to the top two places--namely 1 and 2--would be averaged, giving a rank of 1.5 to the first two finishers. Similarly, the next two would have equal ranks of 3.5 and the last two would have ranks of 5.5. – whuber Jun 4 '18 at 15:03
• thanks, i tried it with the rank function and I saw how R assigns ranks differently from what I expected. – PalimPalim Jun 4 '18 at 15:07

As pointed out by @mdewey I am not already using ranks. If I use the correct ranks results are identical as expected.

a <- c(1,2,3,3)
b <- c(1,2,2,1)
cor(a, b, method="spearman")
# [1] 0.2357023
rank(a)
# [1] 1.0 2.0 3.5 3.5
rank(b)
# [1] 1.5 3.5 3.5 1.5
cor(rank(a), rank(b), method="spearman")
# [1] 0.2357023