Calculating distance for pairs of points within a list

27 Views Asked by At

I have a list of 20 elements, whereby each element is a dataframe containing a pair of spatial points. I am trying to create a function that calculates the distance between each pair of points. See reproducible example at the bottom.

The function I tried to write;

my_fun <- function(x) { 
  x %>%   rowwise() %>%     distHaversine(c(lon,lat))
}
result <- z %>%
  lapply( my_fun )

But I suspect I am doing something wrong, I am new to creating functions.

The list;

list(`3` = structure(list(Wolf = c(3L, 3L), wolfyear = c("3_19", 
"3_19"), Datetime = c("12/23/2019 23:00", "1/15/2020 4:00"), 
    Sex = c(NA_character_, NA_character_), lon = c(-89.68211, 
    -90.28332), lat = c(45.95912, 45.9702), distance = c(NA, 
    46536.9009889411)), row.names = 1:2, class = "data.frame"), 
    `4` = structure(list(Wolf = c(4L, 4L), wolfyear = c("4_18", 
    "4_18"), Datetime = c("12/12/2018 12:00", "4/11/2019 18:00"
    ), Sex = c("M", "M"), lon = c(-88.42661, -88.13358), lat = c(46.2438, 
    45.91429), distance = c(146496.826897965, 43098.4544323091
    )), row.names = 3:4, class = "data.frame"), `8` = structure(list(
        Wolf = c(8L, 8L), wolfyear = c("8_17", "8_17"), Datetime = c("7/20/2017 8:00", 
        "2/4/2018 0:00"), Sex = c("F", "F"), lon = c(-93.26467, 
        -92.57228), lat = c(47.54163, 47.78376), distance = c(431292.434918506, 
        58490.8218082454)), row.names = 5:6, class = "data.frame"), 
    `14` = structure(list(Wolf = c(14L, 14L), wolfyear = c("14_18", 
    "14_18"), Datetime = c("6/28/2018 0:00", "6/25/2018 20:00"
    ), Sex = c("M", "M"), lon = c(-92.88843, -92.93647), lat = c(46.89342, 
    46.80084), distance = c(101941.02681555, 10935.7597586643
    )), row.names = 7:8, class = "data.frame"), `50` = structure(list(
        Wolf = c(50L, 50L), wolfyear = c("50_18", "50_18"), Datetime = c("12/19/2018 2:00", 
        "2/16/2019 16:00"), Sex = c("F", "F"), lon = c(-88.72513, 
        -86.0411), lat = c(45.75233, 46.07726), distance = c(344354.148874818, 
        210985.491411306)), row.names = 9:10, class = "data.frame"), 
    `60` = structure(list(Wolf = c(60L, 60L), wolfyear = c("60_18", 
    "60_18"), Datetime = c("1/11/2019 22:00", "4/13/2019 11:00"
    ), Sex = c("M", "M"), lon = c(-88.36981, -88.20194), lat = c(45.91941, 
    45.91429), distance = c(180930.914047766, 13013.2016027522
    )), row.names = 11:12, class = "data.frame"), `66` = structure(list(
        Wolf = c(66L, 66L), wolfyear = c("66_17", "66_17"), Datetime = c("10/21/2017 12:00", 
        "12/10/2017 21:00"), Sex = c("M", "M"), lon = c(-89.49804, 
        -87.76752), lat = c(45.5085, 46.21571), distance = c(110409.380128141, 
        155541.196345879)), row.names = 13:14, class = "data.frame"), 
    `100` = structure(list(Wolf = c(100L, 100L), wolfyear = c("100_19", 
    "100_19"), Datetime = c("11/22/2019 14:00", "12/1/2019 6:00"
    ), Sex = c("M", "M"), lon = c(-91.27977, -91.12211), lat = c(46.32266, 
    46.1618999999999), distance = c(270514.340115139, 21623.8813426074
    )), row.names = 15:16, class = "data.frame"), `104` = structure(list(
        Wolf = c(104L, 104L), wolfyear = c("104_18", "104_18"
        ), Datetime = c("5/19/2018 16:00", "6/19/2018 13:00"), 
        Sex = c("F", "F"), lon = c(-92.04444, -91.96593), lat = c(46.34149, 
        46.33436), distance = c(73758.2472148184, 6085.89513896184
        )), row.names = 17:18, class = "data.frame"), `147` = structure(list(
        Wolf = c(147L, 147L), wolfyear = c("147_17", "147_17"
        ), Datetime = c("11/22/2017 9:00", "4/12/2018 18:00"), 
        Sex = c("F", "F"), lon = c(-84.89652, -81.83901), lat = c(46.30259, 
        45.81371), distance = c(543346.547621413, 242357.376624363
        )), row.names = 19:20, class = "data.frame"), `153` = structure(list(
        Wolf = c(153L, 153L), wolfyear = c("153_18", "153_18"
        ), Datetime = c("12/4/2018 9:00", "4/10/2019 16:00"), 
        Sex = c("M", "M"), lon = c(-85.78535, -84.34825), lat = c(46.34108, 
        46.34021), distance = c(310307.532679415, 110441.914085739
        )), row.names = 21:22, class = "data.frame"), `160` = structure(list(
        Wolf = c(160L, 160L), wolfyear = c("160_18", "160_18"
        ), Datetime = c("4/15/2018 16:00", "10/20/2018 16:00"
        ), Sex = c("F", "F"), lon = c(-92.62263, -93.83878), 
        lat = c(48.22921, 48.85379), distance = c(658869.589602135, 
        113435.543090328)), row.names = 23:24, class = "data.frame"), 
    `174` = structure(list(Wolf = c(174L, 174L), wolfyear = c("174_18", 
    "174_18"), Datetime = c("4/18/2018 4:00", "11/6/2018 5:00"
    ), Sex = c("M", "M"), lon = c(-88.57494, -87.9441), lat = c(46.69033, 
    46.77762), distance = c(461464.187764928, 49102.0702612771
    )), row.names = 25:26, class = "data.frame"), `190` = structure(list(
        Wolf = c(190L, 190L), wolfyear = c("190_19", "190_19"
        ), Datetime = c("6/15/2019 18:00", "8/17/2019 14:00"), 
        Sex = c("F", "F"), lon = c(-85.3494104538267, -84.3948705483667
        ), lat = c(46.2301021636035, 46.3247776124056), distance = c(207931.100779023, 
        74194.5208803458)), row.names = 27:28, class = "data.frame"), 
    `193` = structure(list(Wolf = c(193L, 193L), wolfyear = c("193_18", 
    "193_18"), Datetime = c("4/17/2018 15:00", "6/16/2018 18:00"
    ), Sex = c("F", "F"), lon = c(-87.4975101273309, -87.9901404342513
    ), lat = c(45.8962267117781, 46.4689460703153), distance = c(244133.375537744, 
    74204.0796827089)), row.names = 29:30, class = "data.frame"), 
    `215` = structure(list(Wolf = c(215L, 215L), wolfyear = c("215_17", 
    "215_17"), Datetime = c("1/25/2018 7:00", "4/13/2018 20:00"
    ), Sex = c("M", "M"), lon = c(-86.1996198035406, -86.5391805034835
    ), lat = c(46.3882461666155, 46.0419980773304), distance = c(137673.227308421, 
    46580.6703930109)), row.names = 31:32, class = "data.frame"), 
    `218` = structure(list(Wolf = c(218L, 218L), wolfyear = c("218_18", 
    "218_18"), Datetime = c("1/3/2019 19:00", "4/13/2019 11:00"
    ), Sex = c("M", "M"), lon = c(-88.3184806212992, -88.2019401985951
    ), lat = c(45.7925136623706, 45.9142906748905), distance = c(140564.75749524, 
    16291.5607408887)), row.names = 33:34, class = "data.frame"), 
    `333` = structure(list(Wolf = c(333L, 333L), wolfyear = c("333_20", 
    "333_20"), Datetime = c("11/29/2020 18:00", "1/20/2021 5:00"
    ), Sex = c("F", "F"), lon = c(-87.5693494005512, -86.8999598865143
    ), lat = c(46.6490327264583, 46.152703593905), distance = c(95174.6287207958, 
    75453.3999905368)), row.names = 35:36, class = "data.frame"), 
    `376` = structure(list(Wolf = c(376L, 376L), wolfyear = c("376_21", 
    "376_21"), Datetime = c("4/16/2021 17:00", "6/2/2021 20:00"
    ), Sex = c(NA_character_, NA_character_), lon = c(-84.655370319338, 
    -84.4471401777404), lat = c(46.3918721256897, 46.1954887287749
    ), distance = c(174749.543217515, 27100.6491001272)), row.names = 37:38, class = "data.frame"), 
    `380` = structure(list(Wolf = c(380L, 380L), wolfyear = c("380_20", 
    "380_20"), Datetime = c("4/15/2020 13:00", "8/19/2020 18:00"
    ), Sex = c("M", "M"), lon = c(-84.8268601982849, -92.7710000236682
    ), lat = c(45.8857321328021, 45.9391997201502), distance = c(45276.2225778408, 
    615057.895089378)), row.names = 39:40, class = "data.frame"))
1

There are 1 best solutions below

0
Allan Cameron On

You can do:

sapply(z, function(d) geosphere::distHaversine(d[c("lon", "lat")]))
#>          3          4          8         14         50         60         66 
#>  46536.901  43098.454  58490.822  10935.760 210985.491  13013.202 155541.196 
#>        100        104        147        153        160        174        190 
#>  21623.881   6085.895 242357.377 110441.914 113435.543  49102.070  74194.521 
#>        193        215        218        333        376        380 
#>  74204.080  46580.670  16291.561  75453.400  27100.649 615057.895