Wednesday, 18 September 2013

Polygon(), Polygons(), SpatialPolygons(), and SpationPolygonsDataFrame()....{spdep}

Polygon(), Polygons(), SpatialPolygons(), and
SpationPolygonsDataFrame()....{spdep}

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)
xycoordinatesF<-data.frame(xycoordinates)
xycoordinatesM<-data.matrix(xycoordinatesF)
pl<-Polygon(xycoordinatesM)
pls<-Polygons(list(pl), ID="1")
Sr<-SpatialPolygons(list(pls))
bho.nb<-poly2nb(list(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,

No comments:

Post a Comment