create a dataset with 2 sets of coordinates in latitude and longitude in 4 columns. then using st_as_sf convert these into 2 geometry columns in r
# Create a dataset with 2 sets of coordinates in latitude and longitude in 4 columnsdata <- data.frame(
id = c(1, 2, 3),
lat1 = c(40.7128, 48.8566, 51.5074),
lon1 = c(-74.0060, 2.3522, -0.1278),
lat2 = c(34.0522, 33.6844, 39.9042),
lon2 = c(-118.2437, -117.8265, 116.4074)
)
# Convert the dataset to a spatial object using st_as_sflibrary(sf)
# Convert the dataset to spatial points data framespatial_data <- st_as_sf(data, coords = c('lon1', 'lat1'), crs = 4326)
# Add the second set of coordinatesspatial_data$geometry <- st_sfc(st_multipoint(cbind(data$lon1, data$lat1),
cbind(data$lon2, data$lat2)), crs = 4326)
# Print the spatial objectprint(spatial_data)