[R-sig-Geo] matching names in the input data with those on the map

Michael Blows blows_michael at yahoo.com
Tue Jul 23 18:52:34 CEST 2013


Hi all, I see someone had earlier posted similar question to the list. But, my problem is that  the counties in the input data don't match those on the map. Codes below:
############
library(spdep)
library(maps)
data<-read.csv("C:/Users/mitra.devkota/Desktop/MT1.csv",header=TRUE) 
names(data)
data$merge <- paste(tolower(data$State),tolower(data$NAME), sep='.')
newmap <- map("county",c("montana"), fill=T, plot=F)
 commaReplace <- function(x) {
    return(gsub(",",".", x))
}
newmap$names <- sapply(newmap$names, commaReplace, simplify=TRUE)
names(newmap$names) <- NULL
length(newmap$names) 
# Merge the data with the geography
index <- match(newmap$names, data$merge)
# printing index gives
>index
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 29 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[51] 51 52 53 54 55 56 NA
#The NA is the problem now.
Any hand is appreciated.

Mike



More information about the R-sig-Geo mailing list