因此,我目前有一段工作代码,可以将一些年度数据作为csv文件导入,并将这些数据合并到一个通用的shapefile中,以便以后在创建地图时使用。虽然我编写的代码有效,但我想尽可能简化它。我目前在global.R
文件中拥有的代码是:
# importing the inputs
countymerge2004 <- read.csv("Maps county level input data 2004.csv") #county level yearly data
countymerge2005 <- read.csv("Maps county level input data 2005.csv")
countymerge2006 <- read.csv("Maps county level input data 2006.csv")
countymerge2007 <- read.csv("Maps county level input data 2007.csv")
countymerge2008 <- read.csv("Maps county level input data 2008.csv")
countymerge2009 <- read.csv("Maps county level input data 2009.csv")
countymerge2010 <- read.csv("Maps county level input data 2010.csv")
countymerge2011 <- read.csv("Maps county level input data 2011.csv")
countymerge2012 <- read.csv("Maps county level input data 2012.csv")
countymerge2013 <- read.csv("Maps county level input data 2013.csv")
countymerge2014 <- read.csv("Maps county level input data 2014.csv")
countymerge2015 <- read.csv("Maps county level input data 2015.csv")
countymerge2016 <- read.csv("Maps county level input data 2016.csv")
countymerge2017 <- read.csv("Maps county level input data 2017.csv")
countymerge2018 <- read.csv("Maps county level input data 2018.csv")
countymerge <- read.csv("Maps county level input data.csv") #county level data for all years
countybor <- readOGR("gz_2010_us_050_00_500k.json", "OGRGeoJSON") #county borders as geoJSON
# cleaning the relevant variables (adding leading zeros)
countymerge2004$STATE <- sprintf("%02d",countymerge2004$STATE)
countymerge2004$COUNTY <- sprintf("%03d",countymerge2004$COUNTY)
countymerge2005$STATE <- sprintf("%02d",countymerge2005$STATE)
countymerge2005$COUNTY <- sprintf("%03d",countymerge2005$COUNTY)
countymerge2006$STATE <- sprintf("%02d",countymerge2006$STATE)
countymerge2006$COUNTY <- sprintf("%03d",countymerge2006$COUNTY)
countymerge2007$STATE <- sprintf("%02d",countymerge2007$STATE)
countymerge2007$COUNTY <- sprintf("%03d",countymerge2007$COUNTY)
countymerge2008$STATE <- sprintf("%02d",countymerge2008$STATE)
countymerge2008$COUNTY <- sprintf("%03d",countymerge2008$COUNTY)
countymerge2009$STATE <- sprintf("%02d",countymerge2009$STATE)
countymerge2009$COUNTY <- sprintf("%03d",countymerge2009$COUNTY)
countymerge2010$STATE <- sprintf("%02d",countymerge2010$STATE)
countymerge2010$COUNTY <- sprintf("%03d",countymerge2010$COUNTY)
countymerge2011$STATE <- sprintf("%02d",countymerge2011$STATE)
countymerge2011$COUNTY <- sprintf("%03d",countymerge2011$COUNTY)
countymerge2012$STATE <- sprintf("%02d",countymerge2012$STATE)
countymerge2012$COUNTY <- sprintf("%03d",countymerge2012$COUNTY)
countymerge2013$STATE <- sprintf("%02d",countymerge2013$STATE)
countymerge2013$COUNTY <- sprintf("%03d",countymerge2013$COUNTY)
countymerge2014$STATE <- sprintf("%02d",countymerge2014$STATE)
countymerge2014$COUNTY <- sprintf("%03d",countymerge2014$COUNTY)
countymerge2015$STATE <- sprintf("%02d",countymerge2015$STATE)
countymerge2015$COUNTY <- sprintf("%03d",countymerge2015$COUNTY)
countymerge2016$STATE <- sprintf("%02d",countymerge2016$STATE)
countymerge2016$COUNTY <- sprintf("%03d",countymerge2016$COUNTY)
countymerge2017$STATE <- sprintf("%02d",countymerge2017$STATE)
countymerge2017$COUNTY <- sprintf("%03d",countymerge2017$COUNTY)
countymerge2018$STATE <- sprintf("%02d",countymerge2018$STATE)
countymerge2018$COUNTY <- sprintf("%03d",countymerge2018$COUNTY)
countymerge$STATE <- sprintf("%02d",countymerge$STATE)
countymerge$COUNTY <- sprintf("%03d",countymerge$COUNTY)
# merge on county statistics to shapefile
countyborder2004 <- merge(countybor,countymerge2004, by = intersect(names(countybor),names(countymerge2004)))
countyborder2005 <- merge(countybor,countymerge2005, by = intersect(names(countybor),names(countymerge2005)))
countyborder2006 <- merge(countybor,countymerge2006, by = intersect(names(countybor),names(countymerge2006)))
countyborder2007 <- merge(countybor,countymerge2007, by = intersect(names(countybor),names(countymerge2007)))
countyborder2008 <- merge(countybor,countymerge2008, by = intersect(names(countybor),names(countymerge2008)))
countyborder2009 <- merge(countybor,countymerge2009, by = intersect(names(countybor),names(countymerge2009)))
countyborder2010 <- merge(countybor,countymerge2010, by = intersect(names(countybor),names(countymerge2010)))
countyborder2011 <- merge(countybor,countymerge2011, by = intersect(names(countybor),names(countymerge2011)))
countyborder2012 <- merge(countybor,countymerge2012, by = intersect(names(countybor),names(countymerge2012)))
countyborder2013 <- merge(countybor,countymerge2013, by = intersect(names(countybor),names(countymerge2013)))
countyborder2014 <- merge(countybor,countymerge2014, by = intersect(names(countybor),names(countymerge2014)))
countyborder2015 <- merge(countybor,countymerge2015, by = intersect(names(countybor),names(countymerge2015)))
countyborder2016 <- merge(countybor,countymerge2016, by = intersect(names(countybor),names(countymerge2016)))
countyborder2017 <- merge(countybor,countymerge2017, by = intersect(names(countybor),names(countymerge2017)))
countyborder2018 <- merge(countybor,countymerge2018, by = intersect(names(countybor),names(countymerge2018)))
countyborder_all <- merge(countybor,countymerge, by = intersect(names(countybor),names(countymerge)))
我尝试编写一个函数来简化整个清理和合并过程,但是由于某种原因,我似乎无法使其正常工作。
yearlyClean <- function(indata, outdata){
indata$STATE <- sprintf("%02d",indata$STATE)
indata$COUNTY <- sprintf("%03d",indata$COUNTY)
outdata <<- merge(countybor,indata, by = intersect(names(countybor),names(indata)))
}
yearlyClean(indata = countymerge2004, outdata = countyborder2004)
任何帮助将不胜感激。我通常在SAS中进行编码,在其中可以使用宏和do循环来自动执行此过程的大部分操作,但是我不熟悉R的语法。