Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:16:06+00:00 2026-06-16T00:16:06+00:00

New to R, and I have a long-ish question: I have a shapefile/map, and

  • 0

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!

EDIT:

I’ve since figured out how to approach this, but for the sake of problem clarity and in response to one question asked in the comments, let me say that a polygon’s neighborhood is the union of itself and every polygon it is adjacent to. In the example I gave above, for polygon ‘A’, that would be the union of polygon’s ‘A’, ‘C’, and ‘E’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:16:07+00:00Added an answer on June 16, 2026 at 12:16 am

    This is what I ended up doing, although it doesn’t appear to be as elegant as @agstudy ‘s:

    for(k in 1:nrow(adjacency00))
     {
      positions <- grep(1,adjacency00[k,])-1
      nghbrd <- UNSD00[c(positions,k),]
    

    etc., thereby creating a frame of adjacent polygons on which to conduct subsequent calculations

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long string, want to break that string in new line after
Have some long base64 string, which must be formated with New Line every 72
One question that I have long asked myself is in object oriented programming,how should
I have the Lat/Long value of New York City, NY; 40.7560540,-73.9869510 and a flat
I came to a new project which have been developed for a quite long
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
I have very strange behaviour of Java Date class: System.out.println(new Date().toGMTString()); long l =
When we have new in C#, that personally I see only as a workaround
I have a long running process that needs to do a lot of queries
i have a long algorithm that i need to put in a report. i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.