nflore
nflore

Reputation: 306

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

Upvotes: 3

Views: 6597

Answers (1)

MonJeanJean
MonJeanJean

Reputation: 2906

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))

Upvotes: 2

Related Questions