I have a large dataframe df whose elements are zipcodes and the associated shapefiles.
There is a territory field df$V2 with around 150 territories.
For each of these territories I am running gTouches to create the adjacency matrix for the associated zip codes. What I want ultimately is a list with 2 fields - the Territory Name (a character) and the Adjacency Matrix (a matrix) - this matrix will be a square matrix but will not be the same size for every element. This list will have 150 elements (1 per territory).
My code looks like this (and I apologize this is definitely embarrassing but I'm a novice):
Adjacency_Matrices<-list()
for(i in 1:length(unique(as.character(df@data$V2))))
{Adjacency_Matrices[[i]][1]<-unique(as.character(df@data$V2))[i]
Adjacency_Matrices[[i]][2]<-
gTouches(df[df@data$V2==unique(as.character(df@data$V2))[i],], byid=TRUE)}
So line 1 is looping through the length of my list of unique territories. Line 2 is defining the first field of the list (a string). Line 3/4 defines the second field of the string (gTouches output is a square matrix).
I get two separate errors:
1) number of items to replace is not a multiple of replacement length
2) Error in Adjacency_Matrices[[1]] : subscript out of bounds
I have googled 1 over and over and can't seem to find a way to fix this. Number 2 I think is something I'm simply missing about how to instantiate lists in R.
Any help is appreciated and let me know if you need any clarification.