Skip to contents

This function takes two data frames or matrices and returns a matrix of pairwise Euclidean distances between the two.

Usage

pairDist(x1, x2 = NULL, na.rm = FALSE)

Arguments

x1

Data frame or matrix one or more columns wide.

x2

Data frame or matrix one or more columns wide. If NULL, then pairwise distances between all points in x1 are calculated.

na.rm

Logical, if TRUE then any rows in x1 or x2 with at least one NA are removed first.

Value

Matrix with nrow(x1) rows and nrow(x2) columns. Values are the distance between each row of x1 and row of x2.

See also

Examples


x1 <- data.frame(x=sample(1:30, 30), y=sort(round(100 * rnorm(30))), z=sample(1:30, 30))
x2 <- data.frame(x=1:20, y=round(100 * rnorm(20)), z=sample(1:20, 20))
pairDist(x1, x2)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]       [,7]
#>  [1,]  23.79075  44.73254 219.05707 325.01231 215.59685 323.44706 243.298171
#>  [2,]  25.31798  45.89118 219.36499 325.24145 215.38106 323.25068 243.164553
#>  [3,]  34.55431  29.06888 181.34498 286.48735 176.64088 283.93309 204.249847
#>  [4,]  76.22336  53.63767 122.49898 228.37031 118.00847 226.01770 146.061631
#>  [5,]  91.97282  69.97142 114.24097 218.55434 110.64809 216.78791 137.240665
#>  [6,] 106.30146  83.34867  92.39048 198.22462  88.29496 196.14790 116.034478
#>  [7,] 139.14740 116.05602  59.04236 165.03030  56.08921 163.44418  83.318665
#>  [8,] 144.73769 121.71278  60.63827 163.42582  59.57348 162.64378  83.743656
#>  [9,] 147.97973 125.06398  58.90671 160.85086  57.04384 159.66841  81.030858
#> [10,] 146.49573 123.50708  53.18834 158.41086  49.16299 156.36176  76.295478
#> [11,] 149.12411 126.32102  50.45790 155.64382  45.09989 153.05555  73.280284
#> [12,] 157.70225 135.02963  45.09989 148.38800  37.81534 145.14131  65.741920
#> [13,] 161.61683 138.48827  42.00000 144.90342  41.85690 144.13188  65.145990
#> [14,] 166.70333 143.66976  38.70400 140.29612  37.28270 138.97482  60.216277
#> [15,] 169.05620 146.35231  36.76955 137.80058  28.91366 134.48420  55.497748
#> [16,] 175.69007 152.53852  25.19921 129.28264  26.43861 128.40950  49.020404
#> [17,] 178.75682 155.63097  29.08608 128.32381  31.40064 127.80454  50.099900
#> [18,] 181.99725 159.07231  25.03997 124.36237  20.46949 121.95491  43.023250
#> [19,] 183.80696 160.82910  28.01785 123.98387  27.00000 122.34786  45.000000
#> [20,] 189.95789 167.24533  29.52965 119.33566  22.89105 115.92239  39.899875
#> [21,] 195.17684 171.99128  16.06238 110.81967  24.69818 110.92340  34.554305
#> [22,] 214.01869 191.06805  17.88854  90.55937  20.49390  88.19297   8.944272
#> [23,] 228.03509 205.12191  31.68596  77.00000  34.29286  74.22937   8.717798
#> [24,] 244.93469 221.90539  49.80964  62.76942  53.74942  61.16371  27.294688
#> [25,] 257.45097 234.34590  62.42596  52.63079  67.86015  53.63767  41.291646
#> [26,] 264.57891 241.70436  68.92024  45.08880  70.92249  39.34463  43.520110
#> [27,] 277.26161 254.17514  79.33473  27.65863  84.32082  29.42788  55.982140
#> [28,] 332.08734 309.05825 134.12681  28.72281 138.32570  31.68596 110.118118
#> [29,] 359.85275 336.71501 162.14191  57.62812 167.24234  63.38770 138.888444
#> [30,] 446.05045 423.07210 248.19549 142.45701 252.02579 144.05902 224.002232
#>             [,8]      [,9]     [,10]      [,11]     [,12]     [,13]     [,14]
#>  [1,] 252.168594 180.20266 208.90428 182.956279 383.09398 321.33938 266.87076
#>  [2,] 252.099187 180.09997 208.35067 182.318403 383.01697 321.04517 266.27242
#>  [3,] 213.187711 141.70745 169.08282 143.139792 343.59715 281.48357 226.51711
#>  [4,] 155.167651  83.44459 111.23399  85.340494 286.25338 224.18965 169.36942
#>  [5,] 145.794376  75.16648 103.22790  77.858847 275.60479 213.91120 159.71537
#>  [6,] 125.008000  53.03772  81.50460  55.614746 256.04882 194.06442 139.44892
#>  [7,]  92.135769  20.61553  50.24938  26.248809 223.11208 161.39703 107.43370
#>  [8,]  91.662424  26.64583  53.34791  33.256578 220.65357 159.51802 106.70052
#>  [9,]  88.977525  25.63201  50.28916  30.740852 217.73608 156.44488 103.34409
#> [10,]  85.146932  13.63818  42.30839  17.606817 216.02315 154.05194  99.61426
#> [11,]  82.516665  14.59452  38.94868  15.132746 213.46897 151.42985  96.82975
#> [12,]  75.026662  13.30413  30.08322   5.000000 205.55048 143.23756  88.05680
#> [13,]  73.013697  15.65248  36.59235  23.430749 202.36848 141.15594  88.46468
#> [14,]  68.154237  16.76305  31.25700  21.189620 197.37021 135.90070  82.71638
#> [15,]  64.583280  17.00000  20.27313   9.433981 194.56875 132.24598  77.05842
#> [16,]  57.026310  18.60108  23.40940  23.622024 187.13097 125.78553  73.10951
#> [17,]  57.280014  25.23886  28.23119  29.748950 185.54514 124.56725  72.78736
#> [18,]  51.458721  24.77902  14.14214  23.021729 181.24569 119.22248  64.90763
#> [19,]  52.440442  28.87906  22.49444  29.359837 180.55470 119.05461  66.09841
#> [20,]  47.864392  35.56684  17.05872  31.384710 175.05713 112.98230  58.49786
#> [21,]  40.755368  38.40573  27.22132  41.509035 168.51409 107.87493  57.70615
#> [22,]  17.972201  55.47071  28.75761  53.935146 148.39474  86.58522  33.85262
#> [23,]   9.110434  69.72087  42.27292  67.860150 134.68853  73.00000  22.22611
#> [24,]  19.339080  86.01163  59.68249  84.581322 118.42297  57.14893  16.15549
#> [25,]  32.526912  98.92421  73.95945  98.295473 107.17276  48.33218  27.76689
#> [26,]  35.397740 105.80643  77.23341 103.126136  98.93938  36.61967  19.13113
#> [27,]  46.572524 118.15668  91.59148 117.102519  85.11757  25.67100  36.93237
#> [28,] 101.019800 173.00578 145.50945 171.379695  30.28201  32.93934  88.17029
#> [29,] 129.510617 200.84073 174.28999 199.822421  15.52417  62.82515 117.45637
#> [30,] 215.039531 287.05052 259.08686 285.071921  84.43933 146.12324 201.24363
#>           [,15]     [,16]     [,17]     [,18]     [,19]     [,20]
#>  [1,] 248.05443 136.40015 302.40040  72.58788 176.03977 234.47175
#>  [2,] 247.34187 136.09555 302.02152  70.41307 175.14851 234.07264
#>  [3,] 207.51145  97.88258 262.34329 110.56672 135.40310 194.57903
#>  [4,] 150.52242  42.53234 205.45072 167.58580  79.43551 138.29317
#>  [5,] 140.96099  31.30495 194.59959 178.80716  68.78227 126.41202
#>  [6,] 120.65239  13.37909 175.18562 197.26885  49.19350 107.68008
#>  [7,]  89.02247  26.47640 142.63239 230.58404  22.40536  75.50497
#>  [8,]  88.69047  29.56349 140.21056 234.02137  23.85372  71.95832
#>  [9,]  85.19977  32.69557 137.06203 236.82272  20.44505  68.82587
#> [10,]  80.91971  31.71750 135.07405 237.12866  11.18034  67.48333
#> [11,]  78.24960  39.00000 132.94360 240.53482  17.83255  67.38694
#> [12,]  69.11584  45.96738 124.54718 248.17131  11.04536  58.93216
#> [13,]  70.68946  45.63989 121.94261 251.72008  18.97367  53.76802
#> [14,]  64.69158  50.52722 116.58473 256.43128  17.49286  48.25971
#> [15,]  58.04309  55.66866 113.35784 259.05019  15.36229  47.52894
#> [16,]  55.62374  60.26608 106.79419 266.54831  26.73948  39.29377
#> [17,]  55.79426  62.71363 105.29957 268.76198  29.89983  37.37646
#> [18,]  46.36809  66.60330 100.04499 272.03676  27.18455  32.55764
#> [19,]  48.43552  67.74954  99.65440 273.33862  30.54505  31.59114
#> [20,]  39.96248  75.33260  93.79765 279.16124  35.15679  29.03446
#> [21,]  42.53234  79.65551  88.89319 285.87585  45.45327  23.25941
#> [22,]  19.20937 100.25468  68.45436 305.38337  62.12890  18.41195
#> [23,]  15.77973 114.52074  55.44367 319.45422  76.15773  25.82634
#> [24,]  24.45404 129.17430  37.66962 335.14474  90.40465  31.08054
#> [25,]  39.69887 141.41428  29.74895 347.53705 103.58089  44.02272
#> [26,]  38.06573 149.85660  18.81489 355.02817 110.15444  53.03772
#> [27,]  54.65345 162.11416  12.08305 368.31644 123.91126  64.84597
#> [28,] 107.14943 217.18425  51.94228 423.19381 178.47689 119.62023
#> [29,] 136.15800 244.25601  80.28699 450.58739 206.17953 146.59127
#> [30,] 220.29072 331.27330 165.31485 537.11637 292.24818 233.49946
pairDist(x1)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,]   0.00000  12.04159  49.17316  98.17332 110.67520 127.42841 160.08123
#>  [2,]  12.04159   0.00000  43.32436  97.87237 109.15127 127.15738 160.34026
#>  [3,]  49.17316  43.32436   0.00000  62.27359  69.43342  89.54329 122.96341
#>  [4,]  98.17332  97.87237  62.27359   0.00000  30.67572  30.82207  63.79655
#>  [5,] 110.67520 109.15127  69.43342  30.67572   0.00000  29.17190  57.73214
#>  [6,] 127.42841 127.15738  89.54329  30.82207  29.17190   0.00000  33.61547
#>  [7,] 160.08123 160.34026 122.96341  63.79655  57.73214  33.61547   0.00000
#>  [8,] 164.10058 163.84749 125.23179  72.09022  56.05355  42.15448  22.15852
#>  [9,] 167.46343 166.80827 127.42841  75.00667  58.24946  45.01111  24.77902
#> [10,] 167.38877 167.01497 128.25365  70.83078  61.69279  40.21194  11.35782
#> [11,] 170.79813 170.68392 132.77801  73.03424  69.26038  43.97727  15.16575
#> [12,] 179.30421 178.51891 139.22643  81.52300  74.73286  52.21111  24.81935
#> [13,] 181.51033 181.44145 142.90556  87.41853  74.15524  56.67451  26.79552
#> [14,] 186.77794 186.30352 146.99660  92.07606  78.33901  61.30253  31.87475
#> [15,] 190.37332 189.39113 149.51254  93.04837  83.77947  63.30877  34.78505
#> [16,] 196.09437 196.22946 157.95252 100.60318  89.91107  69.93568  37.16181
#> [17,] 198.63031 198.51700 159.72476 104.35516  90.80198  73.55270  42.42641
#> [18,] 202.78314 202.08414 162.36071 106.28735  94.86833  75.80897  45.00000
#> [19,] 204.02206 203.33716 163.54510 108.81636  95.08943  78.10890  47.86439
#> [20,] 210.82220 209.58292 169.01479 114.37657 102.15185  84.33267  55.26301
#> [21,] 215.26263 215.52494 177.27380 120.44916 108.65082  89.76636  57.00877
#> [22,] 235.32531 235.34018 196.78923 138.03623 130.14991 108.18503  75.35250
#> [23,] 249.44338 249.46543 210.88860 152.03289 144.37105 122.32743  89.54329
#> [24,] 265.55037 265.09998 225.37746 169.20106 157.23231 138.66867 106.37199
#> [25,] 277.66707 277.39863 237.87602 182.11260 169.18038 151.41004 118.96638
#> [26,] 285.80763 285.21395 245.35281 188.42505 178.27226 158.41086 126.26163
#> [27,] 298.03356 298.10233 259.13317 201.50930 191.05235 171.18995 138.12313
#> [28,] 353.07506 353.07081 313.86303 256.15620 246.01829 226.02212 193.03368
#> [29,] 380.27359 380.34852 341.09529 284.27803 272.48670 253.79913 220.76685
#> [30,] 467.18198 467.09528 427.58508 370.01757 359.87498 340.01912 307.11073
#>             [,8]       [,9]     [,10]     [,11]     [,12]      [,13]      [,14]
#>  [1,] 164.100579 167.463429 167.38877 170.79813 179.30421 181.510330 186.777943
#>  [2,] 163.847490 166.808273 167.01497 170.68392 178.51891 181.441451 186.303516
#>  [3,] 125.231785 127.428411 128.25365 132.77801 139.22643 142.905563 146.996599
#>  [4,]  72.090221  75.006666  70.83078  73.03424  81.52300  87.418534  92.076056
#>  [5,]  56.053546  58.249464  61.69279  69.26038  74.73286  74.155243  78.339007
#>  [6,]  42.154478  45.011110  40.21194  43.97727  52.21111  56.674509  61.302528
#>  [7,]  22.158520  24.779023  11.35782  15.16575  24.81935  26.795522  31.874755
#>  [8,]   0.000000   6.403124  19.02630  31.89044  33.51119  18.788294  24.062419
#>  [9,]   6.403124   0.000000  18.62794  31.87475  31.01612  17.606817  20.832667
#> [10,]  19.026298  18.627936   0.00000  13.37909  16.40122  20.024984  22.912878
#> [11,]  31.890437  31.874755  13.37909   0.00000  13.03840  28.460499  30.364453
#> [12,]  33.511192  31.016125  16.40122  13.03840   0.00000  26.419690  24.657656
#> [13,]  18.788294  17.606817  20.02498  28.46050  26.41969   0.000000   8.124038
#> [14,]  24.062419  20.832667  22.91288  30.36445  24.65766   8.124038   0.000000
#> [15,]  37.429935  33.674916  24.91987  24.53569  12.56981  25.377155  20.149442
#> [16,]  35.468296  34.014703  30.69202  32.63434  28.44293  16.822604  15.000000
#> [17,]  35.142567  32.893768  34.94281  39.82462  34.29286  17.146428  13.564660
#> [18,]  42.731721  39.102430  35.74913  37.05401  27.51363  25.826343  19.104973
#> [19,]  41.279535  37.536649  38.60052  42.53234  33.74907  24.433583  17.521415
#> [20,]  51.971146  47.434165  45.17743  46.36809  35.01428  36.496575  28.879058
#> [21,]  52.962251  51.205468  50.60632  51.94228  46.49731  34.496377  31.400637
#> [22,]  77.129761  74.712783  68.79680  65.13064  58.25805  58.549125  54.055527
#> [23,]  91.241438  88.735562  83.02409  79.03164  72.04165  72.608539  67.985293
#> [24,] 102.722928  99.503769  98.45811  97.59611  88.96629  84.646323  79.164386
#> [25,] 114.043851 111.045036 111.30139 111.08105 102.82509  96.192515  91.109824
#> [26,] 124.791827 121.433109 118.46096 115.88788 107.13543 106.555150 100.866248
#> [27,] 135.959553 133.281657 131.22119 129.01163 121.49897 117.498936 112.756375
#> [28,] 191.115149 188.281704 186.07794 183.31939 175.58474 172.702056 167.779617
#> [29,] 217.092146 214.317521 213.73582 211.97170 204.19109 199.042709 194.273004
#> [30,] 305.067206 302.104287 300.08665 297.05723 289.13838 286.755994 281.710845
#>           [,15]     [,16]     [,17]      [,18]      [,19]     [,20]     [,21]
#>  [1,] 190.37332 196.09437 198.63031 202.783135 204.022058 210.82220 215.26263
#>  [2,] 189.39113 196.22946 198.51700 202.084141 203.337158 209.58292 215.52494
#>  [3,] 149.51254 157.95252 159.72476 162.360710 163.545101 169.01479 177.27380
#>  [4,]  93.04837 100.60318 104.35516 106.287346 108.816359 114.37657 120.44916
#>  [5,]  83.77947  89.91107  90.80198  94.868330  95.089432 102.15185 108.65082
#>  [6,]  63.30877  69.93568  73.55270  75.808970  78.108898  84.33267  89.76636
#>  [7,]  34.78505  37.16181  42.42641  45.000000  47.864392  55.26301  57.00877
#>  [8,]  37.42993  35.46830  35.14257  42.731721  41.279535  51.97115  52.96225
#>  [9,]  33.67492  34.01470  32.89377  39.102430  37.536649  47.43416  51.20547
#> [10,]  24.91987  30.69202  34.94281  35.749126  38.600518  45.17743  50.60632
#> [11,]  24.53569  32.63434  39.82462  37.054015  42.532341  46.36809  51.94228
#> [12,]  12.56981  28.44293  34.29286  27.513633  33.749074  35.01428  46.49731
#> [13,]  25.37716  16.82260  17.14643  25.826343  24.433583  36.49658  34.49638
#> [14,]  20.14944  15.00000  13.56466  19.104973  17.521415  28.87906  31.40064
#> [15,]   0.00000  22.69361  26.87006  16.093477  23.086793  22.44994  37.60319
#> [16,]  22.69361   0.00000  10.04988  15.620499  16.431677  27.47726  20.02498
#> [17,]  26.87006  10.04988   0.00000  16.522712  11.445523  26.19160  18.49324
#> [18,]  16.09348  15.62050  16.52271   0.000000   9.273618  12.12436  23.00000
#> [19,]  23.08679  16.43168  11.44552   9.273618   0.000000  15.26434  20.22375
#> [20,]  22.44994  27.47726  26.19160  12.124356  15.264338   0.00000  28.80972
#> [21,]  37.60319  20.02498  18.49324  23.000000  20.223748  28.80972   0.00000
#> [22,]  48.74423  42.08325  44.51966  37.134889  40.211939  35.88872  29.69848
#> [23,]  62.38590  56.16939  57.98276  50.783856  53.301032  47.87484  42.02380
#> [24,]  77.44030  70.07139  67.90434  63.229740  62.161081  57.48913  52.27810
#> [25,]  91.30717  82.06095  79.12648  76.459139  74.337070  71.19691  63.09517
#> [26,]  96.01042  91.30717  90.38805  83.480537  83.958323  76.79844  74.69940
#> [27,] 110.69779 102.06371 100.84642  96.938125  96.358705  92.01087  83.58229
#> [28,] 164.76043 157.25457 156.00641 151.528875 151.105923 145.66400 138.83804
#> [29,] 193.04404 184.23083 182.04944 179.022345 177.586599 173.19931 165.05151
#> [30,] 278.26786 271.40744 269.96481 265.326214 264.835798 258.82233 252.88930
#>           [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
#>  [1,] 235.32531 249.44338 265.55037 277.66707 285.80763 298.03356 353.07506
#>  [2,] 235.34018 249.46543 265.09998 277.39863 285.21395 298.10233 353.07081
#>  [3,] 196.78923 210.88860 225.37746 237.87602 245.35281 259.13317 313.86303
#>  [4,] 138.03623 152.03289 169.20106 182.11260 188.42505 201.50930 256.15620
#>  [5,] 130.14991 144.37105 157.23231 169.18038 178.27226 191.05235 246.01829
#>  [6,] 108.18503 122.32743 138.66867 151.41004 158.41086 171.18995 226.02212
#>  [7,]  75.35250  89.54329 106.37199 118.96638 126.26163 138.12313 193.03368
#>  [8,]  77.12976  91.24144 102.72293 114.04385 124.79183 135.95955 191.11515
#>  [9,]  74.71278  88.73556  99.50377 111.04504 121.43311 133.28166 188.28170
#> [10,]  68.79680  83.02409  98.45811 111.30139 118.46096 131.22119 186.07794
#> [11,]  65.13064  79.03164  97.59611 111.08105 115.88788 129.01163 183.31939
#> [12,]  58.25805  72.04165  88.96629 102.82509 107.13543 121.49897 175.58474
#> [13,]  58.54912  72.60854  84.64632  96.19252 106.55515 117.49894 172.70206
#> [14,]  54.05553  67.98529  79.16439  91.10982 100.86625 112.75637 167.77962
#> [15,]  48.74423  62.38590  77.44030  91.30717  96.01042 110.69779 164.76043
#> [16,]  42.08325  56.16939  70.07139  82.06095  91.30717 102.06371 157.25457
#> [17,]  44.51966  57.98276  67.90434  79.12648  90.38805 100.84642 156.00641
#> [18,]  37.13489  50.78386  63.22974  76.45914  83.48054  96.93812 151.52888
#> [19,]  40.21194  53.30103  62.16108  74.33707  83.95832  96.35870 151.10592
#> [20,]  35.88872  47.87484  57.48913  71.19691  76.79844  92.01087 145.66400
#> [21,]  29.69848  42.02380  52.27810  63.09517  74.69940  83.58229 138.83804
#> [22,]   0.00000  14.28286  36.01389  49.72927  52.13444  64.17164 118.21167
#> [23,]  14.28286   0.00000  27.58623  40.60788  39.31921  51.24451 104.47009
#> [24,]  36.01389  27.58623   0.00000  15.29706  24.59675  35.34119  89.11229
#> [25,]  49.72927  40.60788  15.29706   0.00000  25.07987  26.28688  78.51751
#> [26,]  52.13444  39.31921  24.59675  25.07987   0.00000  22.22611  69.51259
#> [27,]  64.17164  51.24451  35.34119  26.28688  22.22611   0.00000  55.26301
#> [28,] 118.21167 104.47009  89.11229  78.51751  69.51259  55.26301   0.00000
#> [29,] 147.13939 133.91789 115.91807 103.35860  98.78259  82.99398  32.46537
#> [30,] 232.03664 218.08485 202.67708 191.52023 182.32115 169.38418 114.16217
#>           [,29]     [,30]
#>  [1,] 380.27359 467.18198
#>  [2,] 380.34852 467.09528
#>  [3,] 341.09529 427.58508
#>  [4,] 284.27803 370.01757
#>  [5,] 272.48670 359.87498
#>  [6,] 253.79913 340.01912
#>  [7,] 220.76685 307.11073
#>  [8,] 217.09215 305.06721
#>  [9,] 214.31752 302.10429
#> [10,] 213.73582 300.08665
#> [11,] 211.97170 297.05723
#> [12,] 204.19109 289.13838
#> [13,] 199.04271 286.75599
#> [14,] 194.27300 281.71084
#> [15,] 193.04404 278.26786
#> [16,] 184.23083 271.40744
#> [17,] 182.04944 269.96481
#> [18,] 179.02234 265.32621
#> [19,] 177.58660 264.83580
#> [20,] 173.19931 258.82233
#> [21,] 165.05151 252.88930
#> [22,] 147.13939 232.03664
#> [23,] 133.91789 218.08485
#> [24,] 115.91807 202.67708
#> [25,] 103.35860 191.52023
#> [26,]  98.78259 182.32115
#> [27,]  82.99398 169.38418
#> [28,]  32.46537 114.16217
#> [29,]   0.00000  90.17206
#> [30,]  90.17206   0.00000