0

I am new to R programming. I am currently working on a research project that can improve mstree() in spdep package. I have a question on how to make Polygon(), Polygons(), SpatialPolygons(), and SpationPolygonsDataFrame() to work. Below are the codes I tried:

 library("spdep")
 library("sp")
 require(maptools)
 Dmat<-matrix(nrow=1,ncol=2)
 rownames(Dmat)<-c("s1")
 Dfram<-data.frame(Dmat)

 #diabetesNum is a dataset that I artificially made
 nrows<-nrow(diabetesNum)
 x1matrix<-data.matrix(diabetesNum)
 x1Frame<-data.frame(diabetesNum)
 x1Frame4<-as(x1Frame,"S4")
 x1X<-prcomp(x1Frame)$x

 #Use the first two principal components for coordinates of the points
 xycoordinates<-data.frame(x1X[,1:2])
 row1<-xycoordinates[1,]
 row1M<-data.frame(row1)
 xycoordinates[nrows+1,]<-c(row1M)
 xycoordinates
            PC1         PC2
  1   285.60120   -6.541733
  2   -78.14211  -30.160212
  3  -263.15002   31.236614
  4    51.00528 -143.520521   
  5  -139.55849   27.842465
  6  -244.09755   31.258001
  7   281.18411   12.072960
  8   580.64105   51.538605
  9  -135.05802  -38.923404
 10 -201.08445   47.932800
 11 -137.34100   17.264425
 12  285.60120   -6.541733

 xycoordinatesF<-data.frame(xycoordinates)
 xycoordinatesM<-data.matrix(xycoordinatesF)
 pl<-Polygon(xycoordinatesM)
 pls<-Polygons(list(pl), ID="1")
 Sr<-SpatialPolygons(list(pls))

 bho.nb<-poly2nb(Sr)
 Error in sp$mbxv[i]:(n * 2) : argument of length 0

...and ERROR! what is the wrong with my code above? I searched and searched on the internet to find out the proper way to use Polygon(), Polygons(), SpatialPolygons(), SpatialPolygonsDataFrame(), and poly2nb() yet I just don't know how to make these functions work! I already have gone through R manual but it is not so much of helpl...... one interesting thing though is that when I view the Polygons object, the Polygons slot is empty; when I view Sr, similarly, the SpatialPolygons slot and Polygons slot are empty. thanks,

rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
user2792124
  • 421
  • 3
  • 5
  • 10
  • 1
    Look at [this post](http://gis.stackexchange.com/questions/18311/instantiating-spatial-polygon-without-using-a-shape-file-in-r/18313#18313) by @Spacedman - gives a nice distilled overview. – SlowLearner Sep 18 '13 at 21:29
  • 1
    Include the error. Include a toy dataset to illustrate the problem. – Simon O'Hanlon Sep 18 '13 at 21:29

0 Answers0