Home > Back-end >  Calculating the distance between specific rows contained in an array in R
Calculating the distance between specific rows contained in an array in R

Time:02-04

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, -16.5292634453385, -16.3349610046196, 
-16.1361399024888, -15.950874784594, -15.7656096666993, -15.5830855765793, 
-15.3978204586845, -15.2191727672184, -15.0245491951204, 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, 
-16.5292634453385, -16.3349610046196, -16.1361399024888, -15.950874784594, 
-15.7656096666993, -15.5830855765793, -15.3978204586845, -15.2191727672184, 
-15.0245491951204, 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, -16.5292634453385, -16.3349610046196, 
-16.1361399024888, -15.950874784594, -15.7656096666993, -15.5830855765793, 
-15.3978204586845, -15.2191727672184, -15.0245491951204, 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, 
-16.5292634453385, -16.3349610046196, -16.1361399024888, -15.950874784594, 
-15.7656096666993, -15.5830855765793, -15.3978204586845, -15.2191727672184, 
-15.0245491951204, 30.2524381537838, 30.3531916700196, 30.5272071629858, 
30.7171985365838, 30.8950904282038, 31.0823407740271, 31.2949254546854, 
31.4994281378246, 31.7210031127054, 31.9271974411109, 32.1276826397913, 
-16.9901669093693, -16.7538535507715, -16.5346514436846, -16.3220667630263, 
-16.1441748714063, -15.956924525583, -15.766933151985, -15.5893818716287, 
-15.4270311921735, -15.2476277008611, -15.0923291163014, 30.5009645314475, 
30.5461511455681, 30.6139310667491, 30.7043042949904, 30.8497156996766, 
31.0523272120904, 31.2642971787076, 31.4895019981817, 31.7082224082, 
31.9424840303829, 32.1502759468517, -17.1031334446709, -16.8314380056749, 
-16.5691010208822, -16.3161224902927, -16.0991763716922, -15.9191581663386, 
-15.7297815067818, -15.5723566084887, -15.4312287222064, -15.2956688798444, 
-15.1601090374824, 30.8172708302921, 30.7720842161714, 30.7494909091111, 
30.7720842161714, 30.8398641373524, 30.9751888367062, 31.1725615054113, 
31.3926248805998, 31.6446400170519, 31.901749569188, 32.172869253912, 
-17.1935066729122, -16.9266510267075, -16.6372020734423, -16.3477531201772, 
-16.0770210753187, -15.8607366993609, -15.67975509987, -15.5216019504477, 
-15.408635415146, -15.3272330012426, -15.2730755727841, 31.1787637432574, 
31.0206105938351, 30.8888330929236, 30.8398641373524, 30.8398641373524, 
30.9208789113905, 31.0883905150161, 31.3050981483887, 31.5517528352116, 
31.8314167346389, 32.1276826397913, -17.2160999799726, -16.974927348699, 
-16.7228295660958, -16.4496662137894, -16.1617013114996, -15.9150466246766, 
-15.690985244914, -15.5216019504477, -15.408635415146, -15.3434084073332, 
-15.3182621869047, 31.5854432703434, 31.3568632291687, 31.1228011324445, 
30.9526425582475, 30.8398641373524, 30.8398641373524, 30.9609596987749, 
31.1630700342355, 31.4272901209211, 31.7024192212714, 32.0147161044897, 
-17.1031334446709, -16.9649266317375, -16.776051149134, -16.5607059608164, 
-16.2951234290758, -16.0015434542641, -15.7394059949302, -15.5372956594696, 
-15.4270862896075, -15.3634488010254, -15.3634488010254, 31.8791562621277, 
31.6001897228928, 31.3250995823117, 31.0764791474447, 30.8956386337588, 
30.8172708302921, 30.8624574444128, 31.018057680467, 31.2524247709054, 
31.5291595281513, 31.833969648007, -16.8546070670073, -16.824166905436, 
-16.7524165283979, -16.6167626288326, -16.4133288080863, -16.1409796952799, 
-15.8548864838307, -15.6145280920572, -15.476415336327, -15.408635415146, 
-15.408635415146, 32.03730941155, 31.7970450769797, 31.5449083346073, 
31.2667560348723, 31.0338064869229, 30.871815898616, 30.8172708302921, 
30.8624574444128, 31.0277762258186, 31.2714628106911, 31.5402566562227, 
-16.5383007681626, -16.6204119533106, -16.6738606105246, -16.6512673034643, 
-16.5514966612505, -16.3443194588229, -16.079402011945, -15.8106081664133, 
-15.6048095467055, -15.4990086433874, -15.4538220292667, 32.1502759468517, 
31.9243428762483, 31.7247464943867, 31.4670887366957, 31.200033565032, 
30.9941408881209, 30.8398641373524, 30.8025243777428, 30.8915741206984, 
31.1123073073621, 31.3821035068004, -16.1768078551973, -16.3369657124327, 
-16.4705208469816, -16.5888753806293, -16.6047572040579, -16.4892377553881, 
-16.2729291729331, -15.9882145442037, -15.7184183447654, -15.5428717722223, 
-15.4538220292667, 32.1502759468517, 31.9921227974293, 31.8113763409467, 
31.6220191612746, 31.3754471206027, 31.1137248498512, 30.9080041496818, 
30.798941561751, 30.7970033624241, 30.9473761658217, 31.1561704361971, 
-15.9056881704733, -16.1206181702085, -16.3129548628834, -16.5017248772314, 
-16.5834873822833, -16.5383007681626, -16.3579144028284, -16.1358852795959, 
-15.8581757171604, -15.6400229925818, -15.4990086433874, 32.1954625609723, 
32.0669077849042, 31.9317355824076, 31.7899459534825, 31.574401239706, 
31.2983477049876, 31.0432039008954, 30.8624574444128, 30.7720842161714, 
30.8215348688113, 31.0206105938351, -15.7023484069304, -15.9438697183001, 
-16.169415149038, -16.3789846991442, -16.5383007681626, -16.5834873822833, 
-16.4909213318222, -16.2974882280374, -16.0401516017254, -15.7658642895922, 
-15.5667885645684, 32.2632424821533, 32.172869253912, 32.0375364857258, 
31.901749569188, 31.698409805645, 31.4487480571023, 31.1529752600707, 
30.925785212668, 30.7946775232318, 30.757592386515, 30.8624574444128, 
-15.5216019504477, -15.7906228701553, -16.0410209386595, -16.2779960399002, 
-16.4737549828772, -16.5834873822833, -16.5576988990966, -16.3982887728748, 
-16.1461406194501, -15.8749933860091, -15.611975178689, 32.3310224033343, 
32.240649175093, 32.1343000662199, 32.0147161044897, 31.8565629550673, 
31.6551613908513, 31.3953383596575, 31.1109838220764, 30.8784333250446, 
30.7494909091111, 30.7494909091111, -15.3634488010254, -15.6411859121779, 
-15.8990707440448, -16.1382386675052, -16.3614307103337, -16.5137692617754, 
-16.5834873822833, -16.5262012861847, -16.3735301923118, -16.1117689617911, 
-15.815314942232, 32.4213956315756, 32.3310224033343, 32.2442044423677, 
32.1502759468517, 32.0147161044897, 31.8386488751087, 31.5952558728983, 
31.3112614264656, 31.0422956041922, 30.8413481883277, 30.7268976020508, 
-15.2504822657237, -15.5104658626071, -15.747535021051, -15.9907284978026, 
-16.2319951862794, -16.4432483128196, -16.5736747797284, -16.6060806893436, 
-16.5373924714594, -16.3364450555949, -16.086434626956, 32.4891755527566, 
32.4213956315756, 32.3310224033343, 32.2276884250134, 32.105089332731, 
31.9455656694214, 31.716069262711, 31.4549778436655, 31.1810895824498, 
30.9404181281295, 30.7946775232318, -15.0923291163014, -15.3607353219677, 
-15.6197830734308, -15.8734623064323, -16.1191616679503, -16.3363315185069, 
-16.4980480040364, -16.5834873822833, -16.560894075223, -16.458108302457, 
-16.221994469318, 32.5569554739376, 32.4731331636386, 32.3986693075428, 
32.2858357892136, 32.1671211644176, 32.0147161044897, 31.8198654582159, 
31.593478239261, 31.3129060431304, 31.0487524649311, 30.8624574444128, 
-14.9793625809998, -15.2439313477815, -15.4991416603599, -15.7516935913147, 
-16.0018094882091, -16.2247354970927, -16.4168451155918, -16.560894075223, 
-16.6060806893436, -16.521256025138, -16.3123676975593, 32.6021420880583, 
32.5343621668773, 32.443988938636, 32.3536157103946, 32.233350526137, 
32.0824960256707, 31.8985543930617, 31.6779817720875, 31.4004992838278, 
31.1127614557138, 30.9302373655938, -14.8663960456981, -15.1315405667518, 
-15.4005614864593, -15.6563475533097, -15.9129868194293, -16.1437207230546, 
-16.3607494878063, -16.5361354946598, -16.6060806893436, -16.560894075223, 
-16.35755431168, 32.6925153162996, 32.6021420880583, 32.511768859817, 
32.4167909817926, 32.2982564025707, 32.163745942717, 31.9879252673964, 
31.7575595237519, 31.5069424499042, 31.2131078521995, 30.9980172867748, 
-14.7308362033361, -15.0098302912881, -15.2755895263831, -15.5262066002307, 
-15.7803010218147, -16.02777801697, -16.2713786134716, -16.4791510500557, 
-16.6060806893436, -16.5834873822833, -16.4027409258006, 32.7377019304203, 
32.6473287021789, 32.5594694275366, 32.4665822456963, 32.3536157103946, 
32.2334640632249, 32.0824960256707, 31.88611429982, 31.6590263784529, 
31.4048573795502, 31.1335771291367, -14.6178696680344, -14.8759150655908, 
-15.1350017768231, -15.4052407135609, -15.6671625097711, -15.9128732823414, 
-16.1458194880711, -16.373189581048, -16.5383007681626, -16.6060806893436, 
-16.5157074611023, 32.7828885445409, 32.7151086233599, 32.628799908179, 
32.5343621668773, 32.4267836299217, 32.308429096274, 32.163604856912, 
32.0080516494594, 31.7885949194797, 31.5249505870661, 31.2465436644384, 
-14.5274964397931, -14.8004213068715, -15.0656712961807, -15.3275541326218, 
-15.5839938732826, -15.8359700499655, -16.0505124098353, -16.2738455384689, 
-16.4707089613883, -16.560894075223, -16.5383007681626, 32.8054818516013, 
32.7377019304203, 32.6699220092393, 32.579548780998, 32.4665822456963, 
32.3478011124138, 32.2180558680326, 32.0670018421075, 31.8899712185891, 
31.6682907754529, 31.3821035068004, -14.4371232115518, -14.6913587189407, 
-14.9588290791867, -15.2037061323724, -15.4392247313548, -15.6855696978508, 
-15.9273731808304, -16.1471154246398, -16.3693326622789, -16.5157074611023, 
-16.5383007681626, 32.8506684657219, 32.7828885445409, 32.6925153162996, 
32.6021420880583, 32.511768859817, 32.3988023245153, 32.2818653333564, 
32.1376558080357, 31.9868288562865, 31.7613109742655, 31.495070042102, 
-14.3467499833105, -14.6168673141279, -14.864391337885, -15.1251502144992, 
-15.3859090911133, -15.6373095135242, -15.8870653192702, -16.0990547657719, 
-16.2950683316418, -16.4705208469816, -16.5157074611023, 32.8732617727823, 
32.8054818516013, 32.7151086233599, 32.6247353951186, 32.5371972518553, 
32.4268776871251, 32.308429096274, 32.174286796401, 32.0253980439787, 
31.833969648007, 31.5854432703434, -14.2337834480088, -14.4972833523976, 
-14.7381899497261, -14.9923313999117, -15.2476471807457, -15.4935265878379, 
-15.736038842062, -15.9720505491653, -16.1887192227686, -16.3745325462183, 
-16.4705208469816, 32.8732617727823, 32.8054818516013, 32.7377019304203, 
32.6650432566789, 32.575296153531, 32.4665822456963, 32.3449935761529, 
32.2179228510601, 32.042924484072, 31.8320979571664, 31.5854432703434, 
-14.1434102197675, -14.4212413881233, -14.685837703622, -14.9616480264998, 
-15.2278889586634, -15.4780209932226, -15.7335638482324, -15.9736011086268, 
-16.1937860897357, -16.3594260025206, -16.4705208469816, 32.8958550798426, 
32.8280751586616, 32.7580634554916, 32.6699220092393, 32.579548780998, 
32.4891755527566, 32.3838838952242, 32.2497140466342, 32.105089332731, 
31.9091422753474, 31.6532231915244, -14.0982236056468, -14.3596247450191, 
-14.6201014500234, -14.8863034224178, -15.1383461075869, -15.403623645613, 
-15.6494869150405, -15.8966232989321, -16.1261942829613, -16.3123676975593, 
-16.425334232861, 32.9184483869029, 32.8677797172329, 32.8054818516013, 
32.7260727344588, 32.6473287021789, 32.5546847321794, 32.4323597426745, 
32.308429096274, 32.142523149544, 31.9590356652866, 31.7210031127054, 
-14.0078503774055, -14.2789700621295, -14.5252256980349, -14.7876520134183, 
-15.0635886688289, -15.3205329286629, -15.5784177605299, -15.8091678018199, 
-16.049000810143, -16.2550816014607, -16.425334232861, 32.9636350010236, 
32.8958550798426, 32.8280751586616, 32.7442333684779, 32.6699220092393, 
32.5569554739376, 32.4609282134051, 32.3310224033343, 32.1934027557252, 
32.0233463075638, 31.8113763409467, -13.9400704562245, -14.2156422938743, 
-14.477979278667, -14.7468980723388, -15.0197644997633, -15.2766194290065, 
-15.5272559827389, -15.7506247289218, -15.9960613987147, -16.1907709591835, 
-16.3801476187403, 32.9636350010236, 32.8958550798426, 32.8280751586616, 
32.7472209503131, 32.6699220092393, 32.5643757288139, 32.4602859506468, 
32.3336028654195, 32.1954625609723, 32.035050080844, 31.8565629550673, 
-13.8722905350435, -14.1495734978443, -14.4268564606452, -14.6987238763829, 
-14.9720639320438, -15.2204687037871, -15.4827116313765, -15.7223612519055, 
-15.9572651368467, -16.156473878843, -16.3349610046196, 32.9862283080839, 
32.9184483869029, 32.8506684657219, 32.7755898955849, 32.6925153162996, 
32.6021420880583, 32.4916895063554, 32.3988023245153, 32.271919713829, 
32.1276826397913, 31.9469361833087, -13.8045106138625, -14.0759514299656, 
-14.3341573932116, -14.6025750099302, -14.8676805712147, -15.1297629331145, 
-15.3835281544869, -15.6313343498538, -15.8518243246769, -16.0651258454121, 
-16.289774390499, 32.9862283080839, 32.9184483869029, 32.8619926679691, 
32.7828885445409, 32.7124226930193, 32.6136738845967, 32.511768859817, 
32.4002003871198, 32.2694998174335, 32.1387992477473, 31.969529490369, 
-13.7367306926815, -13.9932255307764, -14.2676458598823, -14.5326849126804, 
-14.8013020548577, -15.0582040127026, -15.3137986229267, -15.5653905019639, 
-15.8090576069518, -16.0527247119397, -16.2671810834386, 33.0088216151442, 
32.9410416939633, 32.885040123381, 32.8054818516013, 32.7377019304203, 
32.6473287021789, 32.5377454574102, 32.4325706791854, 32.308429096274, 
32.172869253912, 32.0147161044897, -13.6915440785609, -13.9656318652355, 
-14.2445984044704, -14.5005725857274, -14.7746603724019, -15.0393897048731, 
-15.2696922822512, -15.5216019504477, -15.765797781106, -16.0118102051707, 
-16.221994469318, 33.0088216151442, 32.9485284573258, 32.8732617727823, 
32.8119662610573, 32.7377019304203, 32.6528107577283, 32.5605658386463, 
32.4665822456963, 32.3536157103946, 32.222668586648, 32.0824960256707, 
-13.6915440785609, -13.9551769999223, -14.2126076834376, -14.4758254162165, 
-14.7204364354571, -14.9738805254504, -15.2242785939547, -15.4607216273052, 
-15.6893016684798, -15.9236687589183, -16.154214548137, 33.0540082292649, 
32.9862283080839, 32.9184483869029, 32.8506684657219, 32.7828885445409, 
32.6925153162996, 32.6021420880583, 32.5072112386356, 32.378495896878, 
32.2497805551204, 32.105089332731, -13.6011708503195, -13.8680930050106, 
-14.1350151597016, -14.4019373143926, -14.6556246162266, -14.8999534638572, 
-15.1575171643451, -15.4131930363274, -15.6548749133867, -15.8965567904459, 
-16.1316212410767, 33.0540082292649, 32.9862283080839, 32.9184483869029, 
32.8506684657219, 32.7612586316179, 32.6754040647887, 32.5829320715309, 
32.4811016240698, 32.363295295977, 32.240649175093, 32.105089332731, 
-13.5559842361989, -13.8300720228734, -14.1041598095479, -14.3650127433653, 
-14.6169062738971, -14.883507297209, -15.1341324398892, -15.3941160367725, 
-15.6474822072273, -15.8856088170119, -16.1316212410767, 33.0766015363252, 
33.0088216151442, 32.9410416939633, 32.8506684657219, 32.779560351442, 
32.6870332607502, 32.6004585116242, 32.4891755527566, 32.376209017455, 
32.2571732612797, 32.1276826397913, -13.5107976220782, -13.7920510407362, 
-14.0733044593943, -14.3319645709919, -14.598604554073, -14.8663960456981, 
-15.1166059997958, -15.3798398702396, -15.6423763804912, -15.8891640842866, 
-16.1316212410767, 33.0766015363252, 33.0088216151442, 32.9410416939633, 
32.8506684657219, 32.7828885445409, 32.697997371849, 32.6122207245582, 
32.511768859817, 32.3988023245153, 32.2803147738949, 32.1502759468517, 
-13.4656110079575, -13.7476397063463, -14.0296684047351, -14.3023386489206, 
-14.5578976415952, -14.7931340689677, -15.0471425021807, -15.3097341098663, 
-15.5730458998486, -15.8208359575507, -16.0638413198957, 33.0991948433856, 
33.0251851356414, 32.9410416939633, 32.8611898395213, 32.7828885445409, 
32.6925153162996, 32.5939741021683, 32.4891755527566, 32.3980270447845, 
32.2858357892136, 32.172869253912, -13.3978310867766, -13.6751805580637, 
-13.9483324993179, -14.2232620742094, -14.4988339118591, -14.756170538171, 
-15.0101238739501, -15.2747201894488, -15.5449705372388, -15.7800349878695, 
-16.0412480128353, 33.0991948433856, 33.0314149222046, 32.9490766628808, 
32.8732617727823, 32.7830215615135, 32.6952563440744, 32.6075300864044, 
32.511768859817, 32.4161016904328, 32.308429096274, 32.1954625609723, 
-13.3752377797162, -13.6519725369621, -13.9094421802466, -14.1828487443937, 
-14.4371232115518, -14.705501868501, -14.9739745826537, -15.2391191037073, 
-15.5043025845302, -15.7380435498753, -15.9960613987147, 33.1217881504459, 
33.0497166420287, 32.9636350010236, 32.8732617727823, 32.7976739568595, 
32.7065529976044, 32.6037867047231, 32.511768859817, 32.4213956315756, 
32.3114637067108, 32.2180558680326, -13.3526444726559, -13.6237641573799, 
-13.880873709516, -14.1469265272731, -14.4145299044915, -14.6716119079106, 
-14.9325313502143, -15.1979029454438, -15.4639557632007, -15.7219071035539, 
-15.9734680916543, 33.1443814575062, 33.0634802205561, 32.9732205294027, 
32.8732617727823, 32.7860837206673, 32.6925153162996, 32.5829710313001, 
32.4891755527566, 32.3988023245153, 32.308429096274, 32.240649175093, 
-13.3074578585352, -13.569105551968, -13.8401116996041, -14.1071003627814, 
-14.3661481142444, -14.6357837479932, -14.8855671024563, -15.1418738261446, 
-15.412832945179, -15.6837920642134, -15.950874784594, 33.1669747645666, 
33.0827177858005, 32.9984608070344, 32.8982279476366, 32.8054818516013, 
32.6977622288408, 32.6021420880583, 32.511768859817, 32.4213956315756, 
32.3315235802876, 32.2632424821533, -13.3074578585352, -13.5724612937839, 
-13.8374647290326, -14.0958507378528, -14.3573378655961, -14.6126227554932, 
-14.8596845620662, -15.1221545638315, -15.3846245655967, -15.6340673087961, 
-15.9056881704733, 33.1895680716269, 33.1217881504459, 33.0314149222046, 
32.9410416939633, 32.8506684657219, 32.7377019304203, 32.6247353951186, 
32.5343621668773, 32.443988938636, 32.3536157103946, 32.2858357892136, 
-13.2396779373542, -13.5060518864903, -13.749832528566, -13.9936131706418, 
-14.2506286655747, -14.4850508534472, -14.7327078941767, -14.9897233891097, 
-15.2467388840425, -15.5037543789753, -15.7701283281114, 33.2121613786872, 
33.1217881504459, 33.0314149222046, 32.9228064826253, 32.8232353658703, 
32.7151086233599, 32.6174757059317, 32.5338804698085, 32.443988938636, 
32.3600725711335, 32.2858357892136, -13.2170846302939, -13.4786187866388, 
-13.7269180901267, -13.9808989746228, -14.2386232408001, -14.4928036507549, 
-14.738095892523, -15.0024375851289, -15.2509364140755, -15.5151450897088, 
-15.7701283281114, 33.2121613786872, 33.1314872159128, 33.0508130531385, 
32.9636350010236, 32.8696124483043, 32.7569865242663, 32.6283847195966, 
32.5343621668773, 32.443988938636, 32.376209017455, 32.308429096274, 
-13.1944913232336, -13.4559119424906, -13.7173325617476, -13.9628243289744, 
-14.1922461583662, -14.4404319247661, -14.6820002647374, -14.9378917998434, 
-15.1952949346416, -15.448821670786, -15.7023484069304), 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")))

CodePudding user response:

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 

CodePudding user response:

With some subsetting apply() works well with dist() too:

apply(curve_array[c(1,11),,], 3, dist)
#> Frame001.txt Frame002.txt Frame003.txt Frame004.txt Frame005.txt Frame006.txt 
#>     2.781455     2.781455     2.781455     2.781455     2.668020     2.548641 
#> Frame007.txt Frame008.txt Frame009.txt Frame010.txt Frame011.txt Frame012.txt 
#>     2.350681     2.121847     1.791864     1.446678     1.192961     1.054892 
#> Frame013.txt Frame014.txt Frame015.txt Frame016.txt Frame017.txt Frame018.txt 
#>     1.074074     1.182647     1.403697     1.644818     1.889481     2.036533 
#> Frame019.txt Frame020.txt Frame021.txt Frame022.txt Frame023.txt Frame024.txt 
#>     2.155975     2.240272     2.380460     2.484956     2.530551     2.537903 
#> Frame025.txt Frame026.txt Frame027.txt Frame028.txt Frame029.txt Frame030.txt 
#>     2.557738     2.580983     2.659684     2.638101     2.697796     2.698458 
#> Frame031.txt Frame032.txt Frame033.txt Frame034.txt Frame035.txt Frame036.txt 
#>     2.700066     2.693820     2.727060     2.718718     2.631126     2.702522 
#> Frame037.txt Frame038.txt Frame039.txt Frame040.txt Frame041.txt Frame042.txt 
#>     2.744878     2.787322     2.758420     2.801023     2.772264     2.772264 
#> Frame043.txt Frame044.txt Frame045.txt Frame046.txt Frame047.txt 
#>     2.793633     2.750915     2.686989     2.715900     2.665723

CodePudding user response:

What do you mean by "distance"? Given your values I am guessing that these are geographic coordinates, and that you want geographic distance on a spheroid, not Euclidean distance.

If so, you could get the distance between the first and 11th row for one matrix like this

library(geosphere)
distGeo(curve_array[1,,1], curve_array[11,,1])
[1] 302457.3

Assuming that the data are in longitude(x)/latitude(y) order, and not the other way around!

And for the whole lot like this

a <- apply(curve_array, 3, \(m) distGeo(m[1,], m[11,]))

head(a)
#Frame001.txt Frame002.txt Frame003.txt Frame004.txt Frame005.txt Frame006.txt 
#    302457.3     302457.3     302457.3     302457.3     290466.7     278119.9  

For Euclidean distance you could use

b <- apply(curve_array, 3, \(m) dist(m[c(1,11), ]))
head(b)
#Frame001.txt Frame002.txt Frame003.txt Frame004.txt Frame005.txt Frame006.txt 
#    2.781455     2.781455     2.781455     2.781455     2.668020     2.548641  

Which is the same result as in Allan Cameron's answer

  • Related