R - how to display data from the slot "coords" in a spatialpolygondataframe object

Viewed 504

I saw a similar question to the one I have in mind but the solution did not work for me so would like to post here for your help.

I am doing an exercise based on a DataCamp course on geospatial data. The below is the code I executed as a start:

library(sp)
library(tigris)
nyc_tracts <- tracts(state="NY",county="New York", cb=TRUE)

nyc_tracts is created as "spatialpolygonsdataframe" object

I tried to look at the first polygon in the list, with the following syntax:

nyc_tracts@polygons[[1]], 

it displays the following:

An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -73.98733  40.71516

Slot "area":
[1] 9.884812e-06

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
           [,1]     [,2]
 [1,] -73.98975 40.71555
 [2,] -73.98915 40.71671
 [3,] -73.98837 40.71645
 [4,] -73.98672 40.71595
 [5,] -73.98501 40.71544
 [6,] -73.98426 40.71521
 [7,] -73.98334 40.71493
 [8,] -73.98245 40.71470
 [9,] -73.98471 40.71451
[10,] -73.98534 40.71446
[11,] -73.98716 40.71431
[12,] -73.98934 40.71413
[13,] -73.99022 40.71440
[14,] -73.98975 40.71555



Slot "plotOrder":
[1] 1

Slot "labpt":
[1] -73.98733  40.71516

Slot "ID":
[1] "156"

Slot "area":
[1] 9.884812e-06

Next, I tried to display slot "area", slot "ID" and slot "plotOrder" etc with commands like the below and they all worked, e.g.

nyc_tracts@polygons[[1]]@area

displays:

[1] 9.884812e-06

However, I cannot get the slot "coord" to work with the same syntax (it seems to be a matrix of 14x2 as shown in the output above), I tried different combinations but they don't work and R gives an error:

nyc_tracts@polygons[[1]]@coords
nyc_tracts@polygons[[1]]@coords[1,1]

error: no slot of name "coords" for this object of class "Polygons"

I also tried using this:

coordinates(nyc_tracts)

but this is not correct, although it does not give an error; it seems to give a list of coordinates from the slot "labpt" from all polygons of the 1nyc_tracts object, and not only the coordinates of the one polygon[[1]] (as displayed above, with 14 rows and 2 columns) I would like to see on my screen.

I am sure there is a very easy solution but I cannot find it. Thanks a lot for your help!!

1 Answers
Related