2

I have a sfc-column with just 6 points. I'd like to make these point to markers in a leaflet-map in R. So I thought that it would be a nice idea to convert them into two columns: x and y. The sfc-column looks like the following. Probably an easy task, but I have no idea how to split up the values from each point into two columns.

Geometry set for 6 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: 9.952508 ymin: 53.57191 xmax: 10.02894 ymax: 53.62756
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
First 5 geometries:
POINT (9.988762 53.5924)
POINT (9.975138 53.57954)
POINT (9.952508 53.57584)
POINT (10.02894 53.57191)
POINT (10.00046 53.59458)

1 Answer 1

3

Short answer: st_coordinates.

Starting with a data frame:

> pts = data.frame(1:10,1:10)

and making a spatial points data frame like yours:

> spts = st_as_sf(data.frame(pts),coords=1:2)
> spts
Simple feature collection with 10 features and 0 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 1 ymin: 1 xmax: 10 ymax: 10
epsg (SRID):    NA
proj4string:    NA
        geometry
1    POINT (1 1)
2    POINT (2 2)
3    POINT (3 3)
4    POINT (4 4)
5    POINT (5 5)
6    POINT (6 6)
7    POINT (7 7)
8    POINT (8 8)
9    POINT (9 9)
10 POINT (10 10)

you can get the coordinates:

> st_coordinates(spts)
    X  Y
1   1  1
2   2  2
3   3  3
4   4  4
5   5  5
6   6  6
7   7  7
8   8  8
9   9  9
10 10 10

as a two-column matrix:

> class(st_coordinates(spts))
[1] "matrix"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.