How to find the absolute pairwise difference among values of a vector in R?


If a vector contains five values then there will be ten pairwise differences. For example, suppose we have five numbers starting from 1, then the pairwise combinations for these values will be (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5). Now to find the absolute pairwise differences, we would be need to find the differences between each of these combinations and take the absolute value of the answer hence the result will be 1, 2, 3, 4, 1, 2, 3, 1, 2, 1.

Example

 Live Demo

x1<-1:3
abs(apply(combn(x1,2),2,diff))

Output

[1] 1 2 1

Example

 Live Demo

x2<-1:10
abs(apply(combn(x2,2),2,diff))

Output

[1] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3
[39] 4 1 2 3 1 2 1

Example

 Live Demo

x3<-0:20
abs(apply(combn(x3,2),2,diff))

Output

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5
[26] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11
[51] 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1
[76] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10
[101] 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6
[126] 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6
[151] 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1
[176] 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5
[201] 1 2 3 4 1 2 3 1 2 1

Example

 Live Demo

x4<-rnorm(12,1,0.75)
x4

Output

[1] 0.5973664 0.5388115 -0.2435155 0.5167396 1.3219034 0.9080164
[7] 0.4023727 0.9431512 1.5896491 0.5385002 0.4772925 1.7824402

abs(apply(combn(x4,2),2,diff))

[1] 0.93873134 0.21594628 0.65896714 0.76184515 1.21649873 0.28351827
[7] 0.42856328 2.15129690 0.25332961 1.01437721 0.37057314 0.72278506
[13] 0.27976420 0.17688619 0.27776739 0.65521307 0.51016805 1.21256557
[19] 0.68540173 1.95310854 1.30930447 0.44302086 0.54589887 1.00055245
[25] 0.06757199 0.21261700 1.93535062 0.03738333 1.23032349 0.58651942
[31] 0.10287801 0.55753159 0.37544887 0.23040386 1.49232976 0.40563753
[37] 1.67334435 1.02954027 0.45465358 0.47832688 0.33328186 1.38945176
[43] 0.50851554 1.77622235 1.13241828 0.93298046 0.78793545 0.93479817
[49] 0.96316912 2.23087594 1.58707187 0.14504502 1.86777864 0.03018866
[55] 1.29789547 0.65409140 1.72273362 0.17523367 1.44294049 0.79913642
[61] 1.89796729 3.16567411 2.52187004 1.26770682 0.62390274 0.64380407

Example

 Live Demo

x5<-rpois(20,5)
x5

Output

[1] 4 4 5 9 4 5 4 5 4 6 4 1 2 3 4 4 5 4 5 6

abs(apply(combn(x5,2),2,diff))

[1] 3 2 0 5 2 3 2 0 6 0 3 1 5 3 1 2 4 2 8 1 3 2 1 0 1 3 3 3 0 4 2 0 2 1 1 1 5
[38] 2 3 0 1 0 2 4 2 1 3 3 1 1 0 2 0 6 5 2 3 2 0 6 0 3 1 5 3 1 2 4 2 8 3 2 3 5
[75] 1 5 2 6 0 2 4 3 1 3 3 1 0 2 4 2 1 3 3 1 1 0 2 0 6 1 3 3 3 0 4 2 0 2 1 1 1
[112] 5 2 4 2 1 3 3 1 1 0 2 0 6 6 0 3 1 5 3 1 2 4 2 8 6 3 7 1 3 5 4 2 4 2 3 1 5
[149] 3 1 2 4 2 8 4 2 0 2 1 1 1 5 6 4 2 3 5 3 9 2 4 3 1 3 3 2 1 1 1 5 1 3 1 7 2
[186] 0 6 2 4 6

Example

 Live Demo

x6<-rexp(12,5)
x6

Output

[1] 0.214630942 0.791414235 0.141469153 0.047114274 0.047027378 0.333190073
[7] 0.002508767 0.016951547 0.124172744 0.163973518 0.260396594 0.010264201

abs(apply(combn(x6,2),2,diff))

[1] 0.306139776 0.329170981 0.275865536 0.002470828 0.016591242 0.966078530
[7] 0.176470691 0.196067564 0.041360872 0.027528765 0.084748488 0.023031206
[13] 0.030274239 0.303668948 0.289548533 0.659938754 0.129669085 0.110072212
[19] 0.264778903 0.278611011 0.221391287 0.053305445 0.326700154 0.312579739
[25] 0.636907549 0.152700290 0.133103417 0.287810109 0.301642216 0.244422493
[31] 0.273394709 0.259274294 0.690212994 0.099394846 0.079797972 0.234504664
[37] 0.248336771 0.191117048 0.014120414 0.963607702 0.173999863 0.193596736
[43] 0.038890045 0.025057937 0.082277661 0.949487288 0.159879449 0.179476322
[49] 0.024769630 0.010937523 0.068157246 0.789607839 0.770010966 0.924717658
[55] 0.938549765 0.881330042 0.019596873 0.135109818 0.148941926 0.091722202
[61] 0.154706692 0.168538799 0.111319076 0.013832108 0.043387616 0.057219723

Example

 Live Demo

x7<-round(runif(20,3,5),0)
x7

Output

[1] 5 4 4 4 4 5 3 4 5 5 3 4 5 4 4 4 5 4 5 4

abs(apply(combn(x7,2),2,diff))

[1] 1 1 1 1 0 2 1 0 0 2 1 0 1 1 1 0 1 0 1 0 0 0 1 1 0 1 1 1 0 1 0 0 0 1 0 1 0
[38] 0 0 1 1 0 1 1 1 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1 0 1
[75] 1 1 0 1 0 0 0 1 0 1 0 2 1 0 0 2 1 0 1 1 1 0 1 0 1 1 2 2 0 1 2 1 1 1 2 1 2
[112] 1 1 1 1 0 1 0 0 0 1 0 1 0 0 2 1 0 1 1 1 0 1 0 1 2 1 0 1 1 1 0 1 0 1 1 2 1
[149] 1 1 2 1 2 1 1 0 0 0 1 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1
[186] 0 1 1 0 1

Example

 Live Demo

x8<-sample(0:9,20,replace=TRUE)
x8

Output

[1] 0 3 5 9 1 3 4 6 5 6 4 5 8 5 2 6 9 9 3 4

abs(apply(combn(x8,2),2,diff))

[1] 3 5 9 1 3 4 6 5 6 4 5 8 5 2 6 9 9 3 4 2 6 2 0 1 3 2 3 1 2 5 2 1 3 6 6 0 1
[38] 4 4 2 1 1 0 1 1 0 3 0 3 1 4 4 2 1 8 6 5 3 4 3 5 4 1 4 7 3 0 0 6 5 2 3 5 4
[75] 5 3 4 7 4 1 5 8 8 2 3 1 3 2 3 1 2 5 2 1 3 6 6 0 1 2 1 2 0 1 4 1 2 2 5 5 1
[112] 0 1 0 2 1 2 1 4 0 3 3 3 2 1 1 0 3 0 3 1 4 4 2 1 2 1 2 1 4 0 3 3 3 2 1 4 1
[149] 2 2 5 5 1 0 3 0 3 1 4 4 2 1 3 6 2 1 1 5 4 3 1 4 4 2 1 4 7 7 1 2 3 3 3 2 0
[186] 6 5 6 5 1

Example

 Live Demo

x9<-sample(rnorm(10),12,replace=TRUE)
x9

Output

[1] 0.43737176 -2.22651843 2.36890565 -0.49296800 -2.22651843 -0.88688207
[7] 2.36890565 -0.41582556 0.74190591 -2.22651843 -0.88688207 0.03418781

abs(apply(combn(x9,2),2,diff))

[1] 0.436617762 1.437561238 1.254929774 1.979603739 0.670434157 1.257986597
[7] 0.670434157 0.621070420 1.855088939 1.254929774 1.979603739 1.000943476
[13] 0.818312012 1.542985977 0.233816396 0.821368836 0.233816396 0.184452658
[19] 1.418471177 0.818312012 1.542985977 0.182631464 0.542042501 0.767127081
[25] 0.179574641 0.767127081 0.816490818 0.417527701 0.182631464 0.542042501
[31] 0.724673965 0.584495617 0.003056823 0.584495617 0.633859354 0.600159165
[37] 0.000000000 0.724673965 1.309169581 0.721617141 1.309169581 1.358533319
[43] 0.124514800 0.724673965 0.000000000 0.587552440 0.000000000 0.049363737
[49] 1.184654782 0.584495617 1.309169581 0.587552440 0.636916178 0.597102342
[55] 0.003056823 0.721617141 0.049363737 1.184654782 0.584495617 1.309169581
[61] 1.234018519 0.633859354 1.358533319 0.600159165 0.124514800 0.724673965

Example

 Live Demo

x10<-sample(rpois(10,2),12,replace=TRUE)
x10

Output

[1] 5 1 2 2 4 0 1 5 5 2 2 1

abs(apply(combn(x10,2),2,diff))

[1] 3 0 3 0 3 3 4 0 4 0 4 3 0 3 0 0 1 3 1 3 1 3 0 3 3 4 0 4 0 4 3 0 0 1 3 1 3 1
[39] 3 3 4 0 4 0 4 0 1 3 1 3 1 1 3 1 3 1 4 0 4 0 4 0 4 4 0 4

Updated on: 08-Oct-2020

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements