I have this data:
df <- data.frame (pc_home = c(1042, 2052, NA, 4021, 9423, NA, 1502, 5942),
pc_work = c(NA, 2105, NA, 4352, 8984, NA, 1495, 6050),
centroid_home = c(c(122239.347627534, 487236.185950724), c(121552.622967901, 487511.344167049), c(NA, NA), c(120168.155075649, 489952.753092173), c(119154.137476474, 489381.429089547), c(NA,NA), c(120723.216386427, 487950.166456445), c(120570.498333358, 487104.749088018))
centroid_work = c(c(NA, NA), c(121337.696586159, 486235.561338213), c(NA, NA), c(123060.850070339, 486752.640463608), c(124354.37048732, 487473.329840357), c(NA,NA), c(123171.113425247, 488458.596501631), c(123952.971290978, 489249.568149519))
)
The centroids were calculated using st_centroid() on a shapefile. The c(NA,NA) were the result of missing postal codes used to calculate centroids.
And I use this code:
library(sf)
df <- df %>%
mutate(dist_hw = st_distance(centroid_home, centroid_work))
No errors, but inspecting the data, I get weird results. In the dataframe view I see no results, and when I try to sort (to see if there are any results), I get this error:
Error in `stop_subscript()`:
! Can't subset elements that don't exist.
x Locations 4324, 7679, 11034, 13428, 16783, etc. don't exist.
i There are only 3355 elements.
I wonder if the error is caused by the NAs or something else?
If it is caused by the NAs, how do I solve that?
All I want is to calculate distance between the points.
st_distance
only apply to "object of class sf, sfc or sfg" – Basticentroid_home
andcentroid_work
sf objects ? – Basti