# [R] kendall tau distance

David L Carlson dcarlson at tamu.edu
Sun Sep 13 22:30:52 CEST 2015

```The function takes a two way table. Assuming your example involves two ordinal vectors c(1,2,3,4,5) and c(3,4,1,2,5), you need:

> xy <- table(c(1,2,3,4,5),c(3,4,1,2,5))
> xy

1 2 3 4 5
1 0 0 1 0 0
2 0 0 0 1 0
3 1 0 0 0 0
4 0 1 0 0 0
5 0 0 0 0 1
> cd <- ConDisPairs(xy)
> cd
\$pi.c
[,1] [,2] [,3] [,4] [,5]
[1,]    3    2    2    1    0
[2,]    2    1    1    2    1
[3,]    2    1    1    2    2
[4,]    1    2    2    3    3
[5,]    0    1    2    3    4

\$pi.d
[,1] [,2] [,3] [,4] [,5]
[1,]    0    1    2    2    3
[2,]    1    2    2    2    2
[3,]    2    2    2    1    1
[4,]    2    2    1    0    0
[5,]    3    2    1    0    0

\$C
[1] 6

\$D
[1] 4

Where C is the number of concordant pairs and D is the number of discordant pairs. Kendall's tau distance is the number of discordant pairs = 4 and the normalized tau is 4/(5*(5-1)/2) = .4.

\$pi.c and \$pi.d are used to compute C and D as follows:

> sum((xy * cd\$pi.c))/2
[1] 6
> sum((xy * cd\$pi.d))/2
[1] 4

David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: Ragia Ibrahim [mailto:ragia11 at hotmail.com]
Sent: Saturday, September 12, 2015 4:21 AM
To: David L Carlson <dcarlson at tamu.edu>; r-help at r-project.org
Subject: RE: [R] kendall tau distance

I have the vectors
running the code

ConDisPairs( data.frame(c(1,2,3,4,5),c(3,4,1,2,5)) )

\$pi.c
[,1] [,2]
[1,]   12    0
[2,]    8    1
[3,]    7    3
[4,]    5    6
[5,]    0   10

\$pi.d
[,1] [,2]
[1,]    0   14
[2,]    3   12
[3,]    7    9
[4,]    8    5
[5,]   10    0

\$C
[1] 69

\$D
[1] 109

I could not find the result related to the result at wiki page in any way..looking for 4 ? dissimilar pairs?

many thanks

----------------------------------------
> From: dcarlson at tamu.edu
> To: ragia11 at hotmail.com; r-help at r-project.org
> Subject: RE: [R] kendall tau distance
> Date: Fri, 11 Sep 2015 15:37:19 +0000
>
> The Wikipedia article gives a simple formula based on the number of discordant pairs. You can get that from the ConDisPairs() function in package DescTools.
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ragia Ibrahim
> Sent: Thursday, September 10, 2015 12:40 PM
> To: r-help at r-project.org
> Subject: [R] kendall tau distance
>
> Dear group
> how to calculate kendall tau distance according to Kendall_tau_distance at wikipedia
>
>
>