# Resolved: Calculating the distance between specific rows contained in an array in R

In this post, we will see how to resolve Calculating the distance between specific rows contained in an array in R

## Question:

I’m trying to calculate the distance between specific points contained in an array in R. My data looks like this:
```curve_array
, , Frame001.txt

[,1]      [,2]
[1,] 30.13947 -16.92239
[2,] 30.34071 -16.72115
[3,] 30.53260 -16.52926
[4,] 30.72348 -16.33496
[5,] 30.92572 -16.13614
[6,] 31.13358 -15.95087
[7,] 31.34144 -15.76561
[8,] 31.52396 -15.58309
[9,] 31.73182 -15.39782
[10,] 31.95565 -15.21917
[11,] 32.17287 -15.02455

, , Frame002.txt

[,1]      [,2]
[1,] 30.13947 -16.92239
[2,] 30.34071 -16.72115
[3,] 30.53260 -16.52926
[4,] 30.72348 -16.33496
[5,] 30.92572 -16.13614
[6,] 31.13358 -15.95087
[7,] 31.34144 -15.76561
[8,] 31.52396 -15.58309
[9,] 31.73182 -15.39782
[10,] 31.95565 -15.21917
[11,] 32.17287 -15.02455

, , Frame003.txt

[,1]      [,2]
[1,] 30.13947 -16.92239
[2,] 30.34071 -16.72115
[3,] 30.53260 -16.52926
[4,] 30.72348 -16.33496
[5,] 30.92572 -16.13614
[6,] 31.13358 -15.95087
[7,] 31.34144 -15.76561
[8,] 31.52396 -15.58309
[9,] 31.73182 -15.39782
[10,] 31.95565 -15.21917
[11,] 32.17287 -15.02455```
And for each slice I am trying to calculate the distance between the points contained in row [1,] and row [11,] (the first and last points).
I have truly gotten nowhere with this (I’ve tried the dist function and the geomorph::interlmkdist function) so any help would be much appreciated. I most recently tried the usedist::dist_subset function but it showed the following error, ‘Error in as.matrix(d)[idx, idx] : no ‘dimnames’ attribute for array’.
I’ve had success in using the distancePointToPoint function but have to manually input the values from rows [1,] and [11,] which given the extent of the array, is not ideal.
Ideally, I want to return an array that looks something like this:
```,, Frame001.txt
[1] 2.781459

,, Frame002.txt
[1] 2.781459

,, Frame003.txt
[1] 2.781459

etc.```
Thank you!
```dput(curve_array)
structure(c(30.1394716184822, 30.3407126170086, 30.5325951613319,
30.7234753517486, 30.9257187041817, 31.1335771291367, 31.3414355540918,
31.5239596442118, 31.7318180691669, 31.9556523747537, 32.172869253912,
-16.9223869881883, -16.7211459896618, ...), dim = c(11L,
2L, 47L), dimnames = list(NULL, NULL, c("Frame001.txt", "Frame002.txt",
"Frame003.txt", "Frame004.txt", "Frame005.txt", "Frame006.txt",
"Frame007.txt", "Frame008.txt", "Frame009.txt", "Frame010.txt",
"Frame011.txt", "Frame012.txt", "Frame013.txt", "Frame014.txt",
"Frame015.txt", "Frame016.txt", "Frame017.txt", "Frame018.txt",
"Frame019.txt", "Frame020.txt", "Frame021.txt", "Frame022.txt",
"Frame023.txt", "Frame024.txt", "Frame025.txt", "Frame026.txt",
"Frame027.txt", "Frame028.txt", "Frame029.txt", "Frame030.txt",
"Frame031.txt", "Frame032.txt", "Frame033.txt", "Frame034.txt",
"Frame035.txt", "Frame036.txt", "Frame037.txt", "Frame038.txt",
"Frame039.txt", "Frame040.txt", "Frame041.txt", "Frame042.txt",
"Frame043.txt", "Frame044.txt", "Frame045.txt", "Frame046.txt",
"Frame047.txt")))```

You can use `apply` along the third margin to apply the distance operation to each slice of your array. This is just a simple Euclidian distance function between the first and 11th rows. The output is a named vector:
```apply(curve_array, 3, function(x) sqrt((x[1, 1]-x[11, 1])^2 + (x[1, 2]-x[11, 2])^2))
#> Frame001.txt Frame002.txt Frame003.txt Frame004.txt Frame005.txt
#>     2.781455     2.781455     2.781455     2.781455     2.668020
#> Frame006.txt Frame007.txt Frame008.txt Frame009.txt Frame010.txt
#>     2.548641     2.350681     2.121847     1.791864     1.446678
#> Frame011.txt Frame012.txt Frame013.txt Frame014.txt Frame015.txt
#>     1.192961     1.054892     1.074074     1.182647     1.403697
#> Frame016.txt Frame017.txt Frame018.txt Frame019.txt Frame020.txt
#>     1.644818     1.889481     2.036533     2.155975     2.240272
#> Frame021.txt Frame022.txt Frame023.txt Frame024.txt Frame025.txt
#>     2.380460     2.484956     2.530551     2.537903     2.557738
#> Frame026.txt Frame027.txt Frame028.txt Frame029.txt Frame030.txt
#>     2.580983     2.659684     2.638101     2.697796     2.698458
#> Frame031.txt Frame032.txt Frame033.txt Frame034.txt Frame035.txt
#>     2.700066     2.693820     2.727060     2.718718     2.631126
#> Frame036.txt Frame037.txt Frame038.txt Frame039.txt Frame040.txt
#>     2.702522     2.744878     2.787322     2.758420     2.801023
#> Frame041.txt Frame042.txt Frame043.txt Frame044.txt Frame045.txt
#>     2.772264     2.772264     2.793633     2.750915     2.686989
#> Frame046.txt Frame047.txt
#>     2.715900     2.665723```