Search code examples
rmatrixapplyargs

calculate distance between samples in dataframe with apply() and user-defined function with multiple args


I have a very large dataset of samples and their geographic locations (>500 samples). I would like to compute the distance between all samples (i.e. make a distance matrix) using two different methods: great circle distance between isolates, and great circle distance using waypoints of human migrations. The former is straightforward with the geosphere package and I have achieved it with the code listed after the sample dataset (n = 10).

test <- structure(list(sample_lon = c(85.1, 101.65, 101.52, 100.5, 77.67, 
78.01, 41.8376999, 136.3070068, 43.0671997, 33.6925011), sample_lat = c(20.95, 
3.11, 3.07, 13.76, 27.49, 27.18, 56.2234001, 49.0937004, 52.3828011, 
-12.3848), Cairo = c(5482676.53004203, 7979572.75185404, 7969372.29645241, 
7300548.40307534, 4535773.29985337, 4577259.89703268, 3035531.48539288, 
8559356.13799981, 2675638.0088102, 4698651.89376045), Istanbul = c(5775286.47479145, 
8414621.15676231, 8406733.79230612, 7555850.43260897, 4744788.34257317, 
4791494.45027787, 1968286.36800993, 7814352.55494176, 1704767.09047958, 
5939243.14729151), PhnomPenh = c(2300013.05256412, 910323.277871997, 
918740.411068992, 487913.521378381, 3302731.94513236, 3256808.88281153, 
7291948.53468698, 5171445.2592504, 7089458.06789339, 8189260.33147658
), AddisAbada = c(5137690.28464086, 6987142.79928228, 6973508.88041575, 
6744246.81620126, 4567281.84480851, 4588137.16563698, 5241878.016939, 
9802623.85641921, 4823403.30909928, 2433156.16935115), UN = c("Southern-Asia", 
"South-Eastern-Asia", "South-Eastern-Asia", "South-Eastern-Asia", 
"Southern-Asia", "Southern-Asia", "Eastern-Europe", "Eastern-Europe", 
"Eastern-Europe", "Eastern-Africa"), continent = c("Asia", "Asia", 
"Asia", "Asia", "Asia", "Asia", "Europe", "Europe", "Europe", 
"Africa")), .Names = c("sample_lon", "sample_lat", "Cairo", "Istanbul", 
"PhnomPenh", "AddisAbada", "UN", "continent"), row.names = c(NA, 
10L), class = "data.frame")

require(geosphere)
test.dvse <- apply(test[c("sample_lon", "sample_lat")], 1, FUN=function(X) distVincentyEllipsoid(X, test[c("sample_lon", "sample_lat")]))

This successfully returns a matrix of the distances between all isolates.

For the waypoint analysis, I created a user-defined function that looks at the continents the samples come from and then calculates the distance between isolates via a series of rules.

dCI <- distVincentyEllipsoid(c(31,30), c(28,41)) #Cairo to Istanbul
dCP <- distVincentyEllipsoid(c(31,30), c(104,11)) #Cairo to Phnom Penh
dIP <- distVincentyEllipsoid(c(28,41), c(104,11)) #Istanbul Phnom Penh

calcWayDist <- function(lon1, lat1, con1, lon2, lat2, con2) {
  if (con1 == con2) {
    dd = distVincentyEllipsoid(c(lon1,lat1), c(lon2,lat2))
  } 
  else{
    if (setequal(c("Africa", "Europe"), c(con1, con2)) == TRUE) {
      if (which("Africa" == c(con1, con2)) == 1) {
        afr = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      }
      else {
        afr = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      }
    }
    else if (setequal(c("Africa", "Asia"), c(con1, con2)) == TRUE) {
      s1 = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
      s2 = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
      dd = s1 + s2
    }
    else if (setequal(c("Africa", "Melanesia"), c(con1, con2)) == TRUE) {
      if (which("Africa" == c(con1, con2)) == 1) {
        afr = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      }
      else {
        afr = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      }
    }
    else if (setequal(c("Europe", "Asia"), c(con1, con2)) == TRUE) {
      s1 = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Cairo
      s2 = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Cairo
      dd = s1 + s2
    }
    else if (setequal(c("Europe", "Melanesia"), c(con1, con2)) == TRUE) {
       if (which("Europe" == c(con1, con2)) == 1) {
          eur = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Istanbul
          mel = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
          dd = eur + dIP + mel
       }
        else {
          eur = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Istanbul
          mel = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
          dd = eur + dIP + mel 
        }
    }
    else if (setequal(c("Asia", "Melanesia"), c(con1, con2)) == TRUE) {
      s1 = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
      s2 = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
      dd = s1 + s2 
    }
  }
 return(dd) 
  }

This works as desired when manually entering values. For example,

calcWayDist(3.6,15.6,"Africa",51.9,31.9,"Asia")

5225325

However, when I try and use the apply function to perform the calculation between all possible isolate pairs I get an error.

test.waypoint <- apply(test, 1, FUN=function(X) calcWayDist(X["sample_lon"], X["sample_lat"], X["continent"], test["sample_lon"], test["sample_lat"], test["continent"]))

Error in .pointsToMatrix(p1) : longitude > 360 In addition: Warning message: In if (con1 == con2) { : the condition has length > 1 and only the first element will be used Called from: .pointsToMatrix(p1)

I next tried 'vectorizing' the input arguments for my user defined function:

calcWayDistbyVec <- function(s1, s2) {
  if (s1[3] == s2[3]) {
    dd = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(as.numeric(s2[1]),as.numeric(s2[2])))
  } 
  else{
    if (setequal(c("Africa", "Europe"), c(s1[3], s2[3])) == TRUE) {
      if (which("Africa" == c(s1[3], s2[3])) == 1) {
        afr = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      }
      else {
        afr = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      }
    }
    else if (setequal(c("Africa", "Asia"), c(s1[3], s2[3])) == TRUE) {
      d1 = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(31,30)) #dist from isolate to Cairo
      d2 = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(31,30)) #dist from isolate to Cairo
      dd = d1 + d2
    }
    else if (setequal(c("Africa", "Melanesia"), c(s1[3], s2[3])) == TRUE) {
      if (which("Africa" == c(s1[3], s2[3])) == 1) {
        afr = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      }
      else {
        afr = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      }
    }
    else if (setequal(c("Europe", "Asia"), c(s1[3], s2[3])) == TRUE) {
      d1 = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(28,41)) #dist from isolate to Cairo
      d2 = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(28,41)) #dist from isolate to Cairo
      dd = d1 + d2
    }
    else if (setequal(c("Europe", "Melanesia"), c(s1[3], s2[3])) == TRUE) {
       if (which("Europe" == c(s1[3], s2[3])) == 1) {
          eur = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(28,41)) #dist from isolate to Istanbul
          mel = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(104,11)) #dist from isolate to Phnom Penh
          dd = eur + dIP + mel
       }
        else {
          eur = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(28,41)) #dist from isolate to Istanbul
          mel = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(104,11)) #dist from isolate to Phnom Penh
          dd = eur + dIP + mel 
        }
    }
    else if (setequal(c("Asia", "Melanesia"), c(s1[3], s2[3])) == TRUE) {
      d1 = distVincentyEllipsoid(c(as.numeric(s1[1]),as.numeric(s1[2])), c(104,11)) #dist from isolate to Phnom Penh
      d2 = distVincentyEllipsoid(c(as.numeric(s2[1]),as.numeric(s2[2])), c(104,11)) #dist from isolate to Phnom Penh
      dd = d1 + d2 
    }
  }
 return(dd) 
  }

This works for an individual calculation:

calcWayDistbyVec(c(3.6,15.6,"Africa"), c(41.8,56.2,"Europe"))

[1] 6435307

But again, when I try and use apply I get an error.

test.waypoint <- apply(test[c("sample_lon", "sample_lat", "continent")], 1, FUN=function(X) calcWayDistbyVec(X, test[c("sample_lon", "sample_lat", "continent")]))

Error in inherits(p, "SpatialPoints") : (list) object cannot be coerced to type 'double' In addition: Warning message: In if (s1[3] == s2[3]) { : the condition has length > 1 and only the first element will be used Called from: inherits(p, "SpatialPoints")

If I do it for a single point it does work though.

test.waypoint.ind <- apply(test[c("sample_lon", "sample_lat", "continent")], 1, FUN=function(X) calcWayDistbyVec(X, c(3.6,15.6,"Africa")))

8702972 11199869 11189668 10520844 7756069 7797556 6438793 12284859 6175273 4535197

If anyone can identify what is not working I would greatly appreciate it! I have looked at posts Apply User Defined Functions, Apply User-Defined Function to Specific Dataframe Columns which helped me arrive at my first distance matrix calculation, but don't seem to be quite applicable here.


Solution

  • I passed this question on to the R Study Group at my University and someone who does not have a Stack Overflow account offered a solution. It does not use apply().

    calcWayDistMODIFIED <- function(lon1, lat1, con1, lon2, lat2, con2) {
      if (con1 == con2) {
        dd = distVincentyEllipsoid(c(lon1,lat1), c(lon2,lat2))
      } else if (con1 == "Africa" & con2 == "Europe") {
        afr = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      } else if (con1 == "Europe" & con2 == "Africa") {
        afr = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
        eur = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Istanbul
        dd = afr + dCI + eur
      } else if (setequal(c("Africa", "Asia"), c(con1, con2))) {
        s1 = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
        s2 = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
        dd = s1 + s2
      } else if (con1 == "Africa" & con2 == "Melanesia") {
        afr = distVincentyEllipsoid(c(lon1,lat1), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      } else if (con1 == "Melanesia" & con2 == "Africa") {
        afr = distVincentyEllipsoid(c(lon2,lat2), c(31,30)) #dist from isolate to Cairo
        mel = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
        dd = afr + dCP + mel
      } else if (setequal(c("Europe", "Asia"), c(con1, con2))) {
        s1 = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Cairo
        s2 = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Cairo
        dd = s1 + s2
      } else if (con1 == "Europe" & con2 == "Melanesia") {
        eur = distVincentyEllipsoid(c(lon1,lat1), c(28,41)) #dist from isolate to Istanbul
        mel = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
        dd = eur + dIP + mel
      } else if (con1 == "Melanesia" & con2 == "Europe") {
        eur = distVincentyEllipsoid(c(lon2,lat2), c(28,41)) #dist from isolate to Istanbul
        mel = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
        dd = eur + dIP + mel
      } else if (setequal(c("Asia", "Melanesia"), c(con1, con2))) {
        s1 = distVincentyEllipsoid(c(lon1,lat1), c(104,11)) #dist from isolate to Phnom Penh
        s2 = distVincentyEllipsoid(c(lon2,lat2), c(104,11)) #dist from isolate to Phnom Penh
        dd = s1 + s2
      }
      return(dd)
    }
    
    distance_function = function(d) {
      mat = matrix(0, ncol = nrow(d), nrow = nrow(d))
      for(i in 1:nrow(mat)) {
        for(j in 1:nrow(mat)) {
          mat[i,j] = calcWayDistMODIFIED(lon1 = d[i,'sample_lon'], lat1 = d[i,'sample_lat'], con1 = d[i,'continent'], lon2 = d[j,'sample_lon'], lat2 = d[j,'sample_lat'], con2 = d[j,'continent'])
        }
      }
      return(mat)
    }