Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Simple question: how to extract the number of vertices in each polygon in R?

I've found one way that works, but this is very slow (when x is a SpatialPolygonsDataFrame):

y <- fortify(x) 
n.verts <- aggregate(y, by=list(y$id), FUN=length)[,2]
x@data$nverts <- n.verts 
share|improve this question
up vote 2 down vote accepted

If the SpatialPolygonsDataFramehas just one part for each polygon, you can use the following:

# test data from administrative boundaries Liechtenstein/Level 1
R> load("LIE_adm1.RData")

# check number of parts
R> sapply(gadm@polygons, function(x) length(x))
 [1] 1 1 1 1 1 1 1 1 1 1 1

# number of vertices
R> sapply(gadm@polygons, function(y) nrow(y@Polygons[[1]]@coords))
 [1]  307  169  383  675   64  572  431  617 1389  425  456
share|improve this answer
That solved it, many thanks! – RobinLovelace Apr 16 '13 at 14:19
ps system.time(sapply(dshe@polygons, function(y) nrow(y@Polygons[[1]]@coords))) tells me your method is several orders of magnitude faster than mine. – RobinLovelace Apr 16 '13 at 14:25
because fortify() handles the more general case. – rcs Apr 16 '13 at 14:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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