Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting data.frame with coordinates variable into sf object with geometry

My data.frame contains two numeric variables called g_lat and g_long that represent the geographic coordinate of the

observations. It looks like this:

variable 1 g_lat g_long
320000 -34.23000 19.42833
600000 16.10000 -22.80000

How can I convert this data.frame in a sf-object, where the variables g_lat;g_long are treated as the geometry column list?

I tried with the st_as_sf function, but an error occurs: only 0's may be mixed with negative subscripts

Thank you very much for your time

like image 212
nflore Avatar asked Aug 28 '26 07:08

nflore


1 Answers

You can specify which columns are the coordinates:

df <- data.frame(v1 = c(320000, 600000),
                 g_lat = c(-34.23000, 16.10000),
                 g_long = c(19.42833, -22.80000))

df_coord <- st_as_sf(df, coords = c(2:3))
like image 114
MonJeanJean Avatar answered Aug 30 '26 22:08

MonJeanJean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!