Tpellirn
Tpellirn

Reputation: 796

How to add a field in Simple feature collection in shapefile?

If I have this shapefile:

  myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
  geo <- readLines(myurl)
  geo <- paste0(geo, collapse = "")
  library(geojsonsf)
  system.time({ sf <- geojson_sf(geo)})
  library(sf) 
  sf

I would like to add a filed 'ID' to sf that give a number unique (starting from one) to each name in the column name in sf

Upvotes: 2

Views: 459

Answers (1)

lovalery
lovalery

Reputation: 4652

Please find below one possible solution

Reprex

  • Code
library(geojsonsf)
library(sf)

sf$NAME <- paste0(sf$NAME,"_", 1:nrow(sf))
  • Output
sf
#> Simple feature collection with 3221 features and 6 fields
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    CENSUSAREA   LSAD         GEO_ID        NAME COUNTY STATE
#> 1     560.100 County 0500000US01029  Cleburne_1    029    01
#> 2     678.972 County 0500000US01031    Coffee_2    031    01
#> 3     650.926 County 0500000US01037     Coosa_3    037    01
#> 4    1030.456 County 0500000US01039 Covington_4    039    01
#> 5     608.840 County 0500000US01041  Crenshaw_5    041    01
#> 6     561.150 County 0500000US01045      Dale_6    045    01
#> 7     777.093 County 0500000US01049    DeKalb_7    049    01
#> 8     945.080 County 0500000US01053  Escambia_8    053    01
#> 9     627.660 County 0500000US01057   Fayette_9    057    01
#> 10    574.408 County 0500000US01061   Geneva_10    061    01
#>                          geometry
#> 1  POLYGON ((-85.38872 33.9130...
#> 2  POLYGON ((-86.03044 31.6189...
#> 3  POLYGON ((-86.00928 33.1016...
#> 4  POLYGON ((-86.34851 30.9943...
#> 5  POLYGON ((-86.14699 31.6804...
#> 6  POLYGON ((-85.79043 31.3202...
#> 7  POLYGON ((-85.57593 34.8237...
#> 8  POLYGON ((-87.16308 30.9990...
#> 9  POLYGON ((-87.63593 33.8787...
#> 10 POLYGON ((-85.77267 30.9946...

Created on 2022-02-28 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions