New to R, and I have a long-ish question:
I have a shapefile/map, and I'm aiming to calculate a certain index for every polygon in that map, based on attributes of that polygon and each polygon that neighbors it.
I have an adjacency matrix -- which I think is the same as a "1st-order queen contiguity weights matrix", although I'm not sure -- that describes which polygons border which other polygons, e.g.,
POLYID A B C D E
A 0 0 1 0 1
B 0 0 1 0 0
C 1 1 0 1 0
D 0 0 1 0 1
E 1 0 0 1 0
The above indicates, for instance, that polygons 'C' and 'E' adjoin polygon 'A'; polygon 'B' adjoins only polygon 'C', etc.
The attribute table I have has one polygon per row:
POLYID TOT L10K 10_15K 15_20K ...
A 500 24 30 77 ...
Where TOT, L10K, etc. are the variables I use to calculate an index.
There are 525 polygons/rows in my data, so I'd like to use the adjacency matrix to determine which rows' attributes to incorporate into the calculation of the index of interest. For now, I can calculate the index when I subset the rows that correspond to one 'bundle' of neighboring polygons, and then use a loop (if it's of interest, I'm calculating the Centile Gap Index, a measure of local income segregation). E.g., subsetting the 'neighborhood' of the Detroit City Schools:
Detroit <- UNSD00[c(142,150,164,221,226,236,295,327,157,177,178,364,233,373,418,424,449,451,487),]
Then record the marginal column proportions and a running total:
catprops <- vector()
for(i in 4:19)
{
catprops[(i-3)]<-sum(Detroit[,i])/sum(Detroit[,3])
}
catprops <- as.data.frame(catprops)
catprops[,2]<-cumsum(catprops[,1])
Columns 4:19 are the necessary ones in the attribute table.
Then I use the following code to calculate the index -- note that the loop has "i in 1:19" because the Detroit subset has 19 polygons.
cgidistsum <- 0
for(i in 1:19)
{
pranks <- vector()
for(j in 4:19)
{
if (Detroit[i,j]==0)
pranks <- append(pranks,0)
else if (j == 4)
pranks <- append(pranks,seq(0,catprops[1,2],by=catprops[1,2]/Detroit[i,j]))
else
pranks <- append(pranks,seq(catprops[j-4,2],catprops[j-3,2],by=catprops[j-3,1]/Detroit[i,j]))
}
distpranks <- vector()
distpranks<-abs(pranks-median(pranks))
cgidistsum <- cgidistsum + sum(distpranks)
}
cgi <- (.25-(cgidistsum/sum(Detroit[,3])))/.25
My apologies if I've provided more information than is necessary. I would really like to exploit the adjacency matrix in order to calculate the CGI for each 'bundle' of these rows.
If you happen to know how I could started with this, that would be great.
and my apologies for any novice mistakes, I'm new to R!