create a dataset with 2 sets of coordinates in latitude and longitude in 4 columns. then using st_as_sf convert these into 2 different geometry columns, and then join them together in r

library(sf)
library(dplyr)

# Create a data frame with 2 sets of coordinates
data <- data.frame(
  id = c(1, 2),
  lon1 = c(-122.4194, -118.2437),
  lat1 = c(37.7749, 34.0522),
  lon2 = c(-117.1611, -121.9467),
  lat2 = c(32.7157, 36.7783)
)

# Use st_as_sf to convert coordinates into geometry columns
points1 <- st_as_sf(data, coords = c("lon1", "lat1"), crs = 4326)
points2 <- st_as_sf(data, coords = c("lon2", "lat2"), crs = 4326)

# Join the two datasets
joined_points <- left_join(points1, points2, by = "id")
516 chars
19 lines
main.r
0 chars
1 lines

related categories

gistlibby LogSnag