Basic question, how do I convert type character LINESTRING variable to a geometry type in sf and then separately, separate the two coordinates into different variables. E.g.
library(tidyverse)
library(sf)
df <- structure(list(geometry = c("LINESTRING (-85.76 38.34, -85.72 38.38)",
"LINESTRING (-85.46 38.76, -85.42 38.76)",
"LINESTRING (-85.89 38.31, -85.85 38.32)"
), var1 = c(4, 5, 6
), var2 = c(1, 2, 3
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
df
# # A tibble: 3 x 3
# geometry var1 var2
# <chr> <dbl> <dbl>
# 1 LINESTRING (-85.76 38.34, -85.72 38.38) 4 1
# 2 LINESTRING (-85.46 38.76, -85.42 38.76) 5 2
# 3 LINESTRING (-85.89 38.31, -85.85 38.32) 6 3
Desired output:
final
# geometry start_lon start_lat end_lon end_lat var1 var2
# -85.76, -85.72, 38.34, 38.38 -85.76 38.34 -85.72 38.38 4 1
# -85.46, -85.42, 38.76, 38.76 -85.46 38.76 -85.42 38.76 5 2
# -85.89, -85.85, 38.31, 38.32 -85.89 38.31 -85.85 38.32 6 3
#so I can quickly plot lines between points
plot(final[, "var1"])
I thought some combination like final <- st_as_sf(df) %>% st_geometry(geometry) would convert it but I cant figure it out. Perhaps sfheaders is useful here?
EDIT
I want an sf type solution and not to manually extract coordinates.
Any suggestions? thanks