Calculating two dimensions Euclidean distance and adding it as a column in the data

Viewed 102

I need to create a variable that measures distance from APP. The center of APP is at the coordinates 1440000 east, 12160000 north. I need to make a new column calculating the euclidean distance between each individual and the center of APP (dist.APP).

This is what I have so far but I'm not sure the equation is correct for two-dimensions:

east = "1440000"

north = "12160000"

b1 = east
b2 = north

dist.APP <- function(a1, b1, a2, b2) {sqrt((a1 - b1)^2 + (a2 - 
b2)^2)
  apply(datwolfcoy, FUN = dist.APP, MARGIN = 2)
  }

Is this how you specify the vectors for the center of APP coordinates?

For adding it as a column for each individual, this didn't quite work and I think it applied to all columns rather than just the individuals since I specified (margin = 2).

I also tried this with no luck:

dist.APP <- data.frame(function(a1, b1, a2, b2) {sqrt((a1 - b1)^2 + (a2 - b2)^2)
}

This is what my data looks like:

ID  PackNumber  StudyArea   CoyoteAncestry(Logit)   PrimaryRds  SecondaryRds    TertiaryRds  Deer   Moose   east    north
49-2    1   Out -0.834473518    0.088   0.499   0.015   0.087   0.112   1358690 12086700
49-3    2   Out -2.408854287    0   0.302   0.188   0   0.382   1346840 12099300
49-4    2   Out -3.896934876    0   0.5 0.164   0.057   0.385   1343380 12100000
49-7    2   Out -2.699548556    0   0.5 0.164   0.057   0.385   1343380 12100000
1 Answers

A possible solution:

df <- data.frame(
         stringsAsFactors = FALSE,
              check.names = FALSE,
                       ID = c("49-2", "49-3", "49-4", "49-7"),
               PackNumber = c(1L, 2L, 2L, 2L),
                StudyArea = c("Out", "Out", "Out", "Out"),
        `CoyoteAncestry(Logit)` = c(-0.834473518,-2.408854287,-3.896934876,
                                    -2.699548556),
               PrimaryRds = c(0.088, 0, 0, 0),
             SecondaryRds = c(0.499, 0.302, 0.5, 0.5),
              TertiaryRds = c(0.015, 0.188, 0.164, 0.164),
                     Deer = c(0.087, 0, 0.057, 0.057),
                    Moose = c(0.112, 0.382, 0.385, 0.385),
                     east = c(1358690L, 1346840L, 1343380L, 1343380L),
                    north = c(12086700L, 12099300L, 12100000L, 12100000L)
      )

euc.dist <- function(x)
{  
  sqrt((1440000-x[1])^2 + (12160000-x[2])^2)
}  
  
df <- cbind(df, dist = apply(df[,c("east", "north")],1, euc.dist))

#>     ID PackNumber StudyArea CoyoteAncestry(Logit) PrimaryRds SecondaryRds
#> 1 49-2          1       Out            -0.8344735      0.088        0.499
#> 2 49-3          2       Out            -2.4088543      0.000        0.302
#> 3 49-4          2       Out            -3.8969349      0.000        0.500
#> 4 49-7          2       Out            -2.6995486      0.000        0.500
#>   TertiaryRds  Deer Moose    east    north     dist
#> 1       0.015 0.087 0.112 1358690 12086700 109472.4
#> 2       0.188 0.000 0.382 1346840 12099300 111190.3
#> 3       0.164 0.057 0.385 1343380 12100000 113734.0
#> 4       0.164 0.057 0.385 1343380 12100000 113734.0
Related