0

I created a data frame (projects) by merging a shape file (US Zip codes) and a dataset. The below line of code returns the X and Y coordinates of my data frame (project)! I Would like to include longitude and latitude in two columns in the project data frame.

However, I just need one lat and long for each zip code. I think the mean of long and lat for each zip code should be sufficient.

    head(st_coordinates(projects))

    > head(st_coordinates(projects))
             X        Y L1 L2 L3
[1,] -71.36374 41.85854  1  1  1
[2,] -71.36449 41.85847  1  1  1
[3,] -71.36473 41.85844  1  1  1
[4,] -71.36580 41.85834  1  1  1
[5,] -71.36573 41.85828  1  1  1
[6,] -71.36562 41.85818  1  1  1

 
M--
  • 25,431
  • 8
  • 61
  • 93
Nader Mehri
  • 514
  • 1
  • 5
  • 21
  • 2
    This has been already answered here: https://stackoverflow.com/a/61745776/9022665 – Jonathan V. Solórzano Jan 20 '23 at 18:43
  • I tried both of the suggested solutions but got an error: "Error in `stopifnot()`: ! Problem while computing `lon = sf::st_coordinates(.)[, 1]`. ✖ `lon` must be size 489 or 1, not 569015." I was thinking to calculate the mean of lats and longs for each zip code! Please advise! – Nader Mehri Jan 20 '23 at 19:04
  • 1
    The 'duplicate' question is slightly different, because it already has a single Point for each polygon. What you need is to first calculate the 'mean' point, then you can follow the results in the other question. Can I suggest you open a new question asking how to get the 'mean' point of the polygon's coordiantes? – tospig Jan 20 '23 at 21:04
  • If the duplicate question's answers don't work, why don't you just use `st_centroid`? – mrhellmann Jan 20 '23 at 22:20
  • I tried st_centroid but got an error: Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, : Loop 0 is not valid: Edge 1616 has duplicate vertex with edge 2124 – Nader Mehri Jan 21 '23 at 02:04
  • 1
    Try `st_make_valid` before `st_centroid`. – mrhellmann Jan 21 '23 at 02:14
  • Got the same error. I just posted a new question: https://stackoverflow.com/questions/75190772/how-to-get-the-mean-point-of-the-polygons-coordiantes – Nader Mehri Jan 21 '23 at 02:16

0 Answers0