How do I calculate Euclidean distance in km from a spatial point that has been converted from a geometry column into a data frame. (The points are points which were derived from a spatial join of spatial data and polygon centroids)
I tried data_sample <- data_sample %>% mutate(distance= distm(cbind(origin_y,origin_x), cbind(dest_y,dest_x),fun = distHaversine)/1000)
and the error i'm getting is Error in .pointsToMatrix(x) : longitude > 360
Below is a sample data frame
data_sample <- data.frame(
origin_x = c(
623613.87,
625678.02,
625678.02,
624359.91,
628136.40,
628136.40,
628136.40,
628136.40,
632329.70
),
origin_y = c(
6438093.66,
6455468.02,
6455468.02,
6449819.06,
6462017.42,
6462017.42,
6462017.42,
6462017.42,
6446947.75
),
dest_x = c(
659627.84,
642136.20,
642136.20,
630395.03,
628422.74,
642136.20,
642136.20,
659627.84,
659627.84
),
dest_y = c(
6473200.36,
6456562.78,
6456562.78,
6451979.98,
6459817.02,
6456562.78,
6456562.78,
6473200.36,
6473200.36)
)
Is there any reason not to use the
sf
builtin functionst_distance
??And responding to the additional comment from william-g-k: