Hello,
I've had the same problems when starting probably your polygon has more
units than data. you have to check if you need to delete that extra unit
that gives you no data or merge it with other. If you need to merge it,
check Applied Spatial Data Analysis with R  by Roger S. Bivand, Edzer
Pebesma and V. Gómez-Rubio section 5.4.1
But if you need to delete the units that you don't have data for, you can
add the data to the shapefile and you can use this code that I found once
by Benoit Delbecq:

library(maptools)
###### POLYGON SHAPEFILE

## example shapefile: columbus.shp

# import your shapefile using readShapePoly
shape <- readShapePoly("columbus.shp",IDvar="POLYID")

# read-in the table containing the data the you want to add to your
shapefile
  # make sure that the table contains a column with a unique identifier
corresponding to IDvar
tab <- read.table("join_example.txt",header=TRUE)

# creates a vector of all polygon IDs
  #note that it is the "character" version of POLYID (in this example)
shpID <- sapply(slot(shape,"polygons"),function(x) slot(x,"ID"))

# make the ID variable from the data table into class "character"
tabID <- as.character(tab$ID)

# match the two unique identifiers
  # match() needs both its arguments to be of the same class. Here:
"character"

  #### if you want to keep all polygons
    tab <- tab[match(shpID,tabID),]
      # matches both IDs and replaces nomatch values by NA
    rownames(tab) <- shpID
    shape.mod <- spCbind(shape,tab)

  #### if you want to delete polygons with no matching data in the table
    tab <- tab[shpID%in%tabID,]
    shape.mod <- shape.mod[shpID%in%tabID,]
      # matches and removes the non-matching locations from the table and
the original shapefile
    shape.mod <- spCbind(shape.mod,tab)

#shape.mod@data
# writes a new shapefile in the current working directory
writePolyShape(shape.mod,"shape_mod")
Hope this helps you solve your problem.
Juan


On Thu, Jan 30, 2014 at 2:42 AM, Roger Bivand <Roger.Bivand@nhh.no> wrote:

> On Thu, 30 Jan 2014, Lareina La Flair wrote:
>
>  Good evening,
>>
>> I'm new to spatial analysis R and therefore to this group but am thankful
>> for any level of feedback on my question that follows.  I've encountered
>> an
>> error in calculating Moran's I using the spdep package in R and am unable
>> to isolate the problem on my own:
>>
>> *Error in moran.test(AODED$adcount/AODED$POP100, nb2listw(BC_nb, style =
>> "B")) : objects of different length*
>>
>> How might I begin solving this problem?
>>
>> My code, adapted from prior posts on this subject:
>>
>> *# load data for 70 area units*
>>
>> *AODED<-read.csv("AODEDlags_R.csv",header=TRUE)
>> *
>>
>>
>> *BC <- readShapePoly("StudyareaZC11.shp") *
>>
>> *# create a weight matrix*
>>
>> *BC_nb <- poly2nb(as(BC, "SpatialPolygons"))
>> X <- nb2mat(BC_nb,zero.policy=TRUE)
>> n<-length(BC_nb)
>> *
>>
>> *Nbr.matrix<-matrix(0,n,n) for(i in 1:n) *
>>
>> *  Nbr.matrix[i,BC_nb[[i]]]<-1**> BC_nb*
>>
>> I have 70 area units, yet the weight matrix counts 71:
>>
>> *Neighbour list object:*
>> *Number of regions: 71 *
>> *Number of nonzero links: 320 *
>> *Percentage nonzero weights: 6.347947 *
>> *Average number of links: 4.507042 *
>>
>
> The very odd appearance of your posting is caused by rendering from
> (forbidden) HTML. Only post plain text, please - HTML postings can contain
> malicious payloads and are not permitted).
>
> If you have 71 entities in BC, you'll get 71 regions in BC_nb for obvious
> reasons. You should combine the attribute data object with the geometries -
> see the vignette in the maptools package:
>
> library(maptools)
> vignette("combine_maptools")
>
> In this way, you should be able to work out which of your 70 observations
> in AODED match the 71 geometries in BC (hopefully you have unique IDs in
> both the CSV and the shapefile that can be matched). You are the only
> person who can do this, as a random deletion of a geometry by the software
> (to make things easier) doesn't make any sense.
>
>
>
>> Any insights on this problem and/or references to prior posts are welcome.
>>
>
> Prior posts may not be any guidance unless you understand what they are
> doing. You references to nb2mat, and your very odd if() loop suggest that
> you have to try to understand harder, as the steps involving X and
> Nbr.matrix are neither clear (maybe the HTML problem) nor necessary. If you
> refer to a prior post, do include its unique URL, preferably by thread from:
>
> https://stat.ethz.ch/pipermail/r-sig-geo/
>
> so that we can see what you are referring to. Reading the help pages of
> the functions carefully is also always sensible. Always also check the
> sizes of input objects; doing:
>
> dim(BC)
> dim(AODED)
>
> after reading them would have alerted you to the size problem. Usually
> also look at the output of summary() of input objects (also maybe str() or
> head() for non-Spatial objects), to be sure that they are what you think
> they are.
>
> Hope this helps,
>
> Roger
>
>
>  Thank you.
>>
>> Respectfully,
>> Lareina
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand@nhh.no
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Juan Tomás Sayago Gómez
Graduate Research Assistant
West Virginia University - RRI
886 Chestnut Ridge Road, Room 520
P.O. Box 6825
Morgantown, WV 26506-6825

	[[alternative HTML version deleted]]

