I am trying to plot data by FIPS code using county shapes files. library(data.table) > library(rgdal) > library(colourschemes) > library(RColorBrewer) > library(maptools) > library(maps) > library(ggmap)
I have data by FIPS code which looks like this: > > > dput(head(max_change)) > structure(list(FIPS = c("01001", "01003", "01005", "01007", "01009", > "01011"), pred_hist = c(5.68493780563595e-06, 5.87686839563543e-06, > 5.68493780563595e-06, 5.84476370329784e-06, 5.89156133294344e-06, > 5.68493780563595e-06), pred_sim = c(5.60128903156804e-06, > 5.82369276823497e-06, > 5.60128903156804e-06, 5.75205304048323e-06, 5.80322399836766e-06, > 5.60128903156804e-06), change = c(-1.47141054005866, -0.904829303986895, > -1.47141054005866, -1.58621746782168, -1.49938750670105, -1.47141054005866 > )), .Names = c("FIPS", "pred_hist", "pred_sim", "change"), class = > c("data.table", > "data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: > 0x0000000000110788>) I add leading zeroes by: max_change <- as.data.table(max_change) max_change$FIPS <- sprintf("%05d",as.numeric(max_change$FIPS)) I downloaded shapefiles from here: ftp://ftp2.census.gov/geo/tiger/TIGER2014/COUNTY/. I obtain the FIPS codes from the shapefiles and order them using: shapes_fips <- shapes$GEOID > shapes_fips <- as.data.table(shapes_fips) > setnames(shapes_fips, "shapes_fips", "FIPS") > shapes_fips <- shapes_fips[with(shapes_fips, order(FIPS)), ] > shapes_fips$FIPS <- as.character(shapes_fips$FIPS) Then I merge the FIPS codes with my original dataset using: > > merged_data <- merge(shapes_fips,max_change,by="FIPS",all.X=T, all.y=T) > merged_data <- as.data.table(merged_data) Which looks like this: structure(list(FIPS = c("01001", "01003", "01005", "01007", "01009", > "01011"), pred_hist = c(5.68493780563595e-06, 5.87686839563543e-06, > 5.68493780563595e-06, 5.84476370329784e-06, 5.89156133294344e-06, > 5.68493780563595e-06), pred_sim = c(5.60128903156804e-06, > 5.82369276823497e-06, > 5.60128903156804e-06, 5.75205304048323e-06, 5.80322399836766e-06, > 5.60128903156804e-06), change = c(-1.47141054005866, -0.904829303986895, > -1.47141054005866, -1.58621746782168, -1.49938750670105, -1.47141054005866 > )), .Names = c("FIPS", "pred_hist", "pred_sim", "change"), sorted = > "FIPS", class = c("data.table", > "data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: > 0x0000000000110788>) But when I try to merged data back to the SpatialPolygonsDataFrame called shapes, I get the following error: shapes$change <- merged_data$change Error in `[[<-.data.frame`(`*tmp*`, name, value = c(-1.47141054005866, : > replacement has 3109 rows, data has 3233 Apologies for the messy example, what am I doing wrong? Any help will be greatly appreciated. Thank you! Sincerely, Shouro [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.