TwentyandChange
TwentyandChange

Reputation: 1

Comparing every latitude and longitude in a dataframe

Probably in over my head here as I'm still learning R...

I have a dataframe containing a column with longitude values and another column with corresponding latitude values. I want to find long/lat locations that are within a defined distance from any other long/lat location in the dataframe - e.g., all locations that are within, say, .005 distance (both longitudinally and latitudinally) from any other location.

I hope that's a clear description. Thanks in advance for any help.

I haven't really tried anything as I'm unsure of how to proceed in R.

Upvotes: 0

Views: 38

Answers (1)

Eonema
Eonema

Reputation: 1320

This probably isn't exactly what you're looking for, but hopefully it will help point you in the right direction:

# some example data
df <- data.frame(
  lon = c(-114.0719, -114.0723, -114.0754, -114.0801, -114.0755),
  lat = c(  51.0447,   51.0404,   51.0505,   51.0303,   51.0400)
)
# define the maximum distance
MAX_DIST <- 0.005

# calculate a matrix of distances between points
dist_mat <- dist(
    df,
    method = "maximum"
  ) |>
  as.matrix()
# set the diagonals to inifinity, so they won't be counted
diag(dist_mat) <- Inf

# for each point, check if the distance to any other point is
#   less than the threshhold
is_nearby <- apply(dist_mat < MAX_DIST, 1, any)

# filter the data frame by which points have nearby neighbors
df_filtered <- df[is_nearby,]

I interpreted your question as looking to select all points that are within both 0.005 degrees longitude and 0.005 degrees latitude of any other point - for this we can use dist with method = "maximum". If you're instead looking for the actual distance, check out the package sf. You'd need to convert df to vector geometry using st_point, then use st_distance in place of dist.

If the purpose of any of these functions is unclear, you can type ? followed by the function name in RStudio to get the documentation, for example, ?apply for help on apply().

Upvotes: 1

Related Questions