0

I am in need of assistance, any guidance is greatly appreciated!

My goal is to create a function that computes the spatial moving average of adjacent neighbors of a polygon shapefile utilizing the existing function poly2nb(). The function must be flexible so that it may call upon any attribute within the working shapefile. I am not sure how to fix the error I am receiving: Error in .subset2(x, i, exact = exact) : no such index at level 3, when I attempt to call my function. I am at a complete loss as to how to fix this issue.

I am working with a shapefile of Chicago neighborhood data, mulypolygon, containing the attributes:

•Community Area ID
•Community Name
•Tract Cnt
•Pop2014
•Hisp14P
•PerCInc14
•Assault
•DiabetM
•FirearmM
•LungCancer

My Function A

a_spmvavg <- function(shapefile, my_attribute) {
    nbs = poly2nb(shapefile)
    a_matrix = nb2mat(nbs, style='W', zero.policy=TRUE)
    a_val = shapefile[[my_attribute]]
    a_average1 = a_matrix%*%a_val
    a_newdf = cbind(shapefile, a_average1)
    return (a_newdf)
}

Attempt to call function:

a_spmvavg(chicagoshp,chicagoshp$Assault)

Error output

Error in .subset2(x, i, exact = exact) : no such index at level 3

4.(function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, i, exact = exact))(x, ..., exact = exact)

3.[[.data.frame(shapefile, my_attribute)

2.shapefile[[my_attribute]]

1.a_spmvavg(chicagoshp, chicagoshp$Assault)

  • Your first step should be to debug the function to find the line that's causing the error. e.g, do these steps in order: 1. `nbs <- poly2nb(chicagoshp)`, 2. `a_matrix <- nb2mat(nbs, style='w', zero.policy=TRUE)`, 3. `a_val <- chicagoshp[[chicagoshp$Assault]]`, ... etc – SymbolixAU Oct 10 '22 at 04:36
  • @SymbolixAU I appreciate your assistance! I ran through each line individually and my issue is when I attempt to attach the desired attribute value from the shapefile to a new variable. a_val = chicagoshp[[chicagoshp$Assault]] I am not sure how to go about fixing this particular issue. I am still new to R so I apologize for my ignorance if this is an obvious fix. From my understanding, when I read the shapefile using st_read(), the shapefile behaves as a dataframe. Is this true? I am unsure of another method to attach the shapefile attribute to a variable. Thank you for the guidance! – affectionate_eye079 Oct 10 '22 at 16:37

1 Answers1

0

You've called it like this:

a_spmvavg(chicagoshp,chicagoshp$Assault)

and your function is defined like this:

a_spmvavg <- function(shapefile, my_attribute) {

So when you get here:

a_val = shapefile[[my_attribute]]

...you are doing chicagoshp[[chicagoshp$Assault]]. Is that what you want?

Or should you pass the name of the column, like: a_spmvavg(chicagoshp,"Assault") in which case shapefile[[myAttribute]] evaluates to chicagoshp[["Assault"]] which gets you the column values as a vector.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • At first, I thought I should call my function by chicagoshp[[chicagoshp$Assault]] in order to produce the desired attribute. However, utilizing your suggestion, it appears the proper way to get the desired column for the specified attribute is by chicagoshp[["Assault"]]. This has eliminated the error output I was receiving and now I am able to move forward with my analysis. Thank you for your input, very grateful! – affectionate_eye079 Oct 10 '22 at 19:40