Extract the value from dataset

Hello,
After using the sf::st_centroid function point<-st_centroid(map), I get the following output

   CODE   geometry
1	T1	c(2.3428828903353, 48.8566258524875)
2	T2	c(2.28399910670902, 48.7819047955817)
3	T3	c(2.21992924736305, 48.8189253283551)
4	T4	c(2.21085169860309, 48.8750398323371)
5	T5	c(2.26906485768456, 48.9343606880188)
6	T6	c(2.36329387765033, 48.9356023370997)
7	T7	c(2.50700090863566, 48.9535727259267)
8	T8	c(2.44397382669959, 48.8877991017321)
9	T9	c(2.54231641904574, 48.8827576906768)
10	T10	c(2.48877839063982, 48.8223141349327)
11	T11	c(2.52861726117573, 48.7611485070806)
12	T12	c(2.38244027640969, 48.7476770877747)

I would like a new table that adds two columns X and Y and in the new columns extract the values of c(x,y) that will be distributed in the columns X and Y.

I've tried the following

  pts<-point %>%
         mutate(Xi=geometry(c[1])) %>%
         mutate(Yi=geometry(c[2])) 

The error message is : Error in c[1] : objet de type 'builtin' non indi├žable
Do you have any idea how I can do that?
Thanks in advance

The function st_coordinates in sf is what you are looking for. Look at the example below:

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
#> 
#> Attaching package: 'tigris'
#> The following object is masked from 'package:graphics':
#> 
#>     plot

options(tigris_class = "sf")

StateSF <- tigris::states(cb=TRUE)

StateSFCentroid <- StateSF %>%
  sf::st_centroid()
#> Warning in st_centroid.sf(.): st_centroid assumes attributes are constant
#> over geometries of x
#> Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
#> of_largest_polygon): st_centroid does not give correct centroids for
#> longitude/latitude data


pts <- st_coordinates(StateSFCentroid)

head(pts)
#>            X        Y
#> 1  -80.61371 38.64259
#> 2  -89.19839 40.06503
#> 3  -76.77245 39.03691
#> 4 -114.65937 44.38908
#> 5  -72.66265 44.07519
#> 6  -72.72570 41.62028
head(StateSFCentroid$geometry)
#> Geometry set for 6 features 
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: -114.6594 ymin: 38.64259 xmax: -72.66265 ymax: 44.38908
#> epsg (SRID):    4269
#> proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
#> First 5 geometries:
#> POINT (-80.61371 38.64259)
#> POINT (-89.19839 40.06503)
#> POINT (-76.77245 39.03691)
#> POINT (-114.6594 44.38908)
#> POINT (-72.66265 44.07519)

Created on 2019-12-28 by the reprex package (v0.3.0)

thanks.

But I want also keep the column CODE. When I've made st_coordinates I have only the coordinate X,Y. Is it possible to keep the column CODE?

I used cbind and it's OK

pt_centroid<-map %>% sf::st_centroid()
pts<-st_coordinates(pt_centroid)
p<-cbind(pts,pt_centroid)

Thanks to your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.