rgeometryfipscensustigris

How to assign the FIPS codes to multiple Counties by name?


New R user looking to assign the FIPS code to the counties within a dataset. I have multiple point data with a county name attached to the information, and I want to assign the appropriate FIPS code to all counties within the dataset.

Data example:

State StateFIPS County
DE 10 Sussex

DE 10 Sussex

DE 10 Kent

DE 10 Sussex

I have been able to attach the FIPS by state using: DEdata$StateFIPS <- fips(DEdata$State)

But I am unable to use this fips function for the County level. Is there anyway to assign the FIPS code for multiple counties within the state?


Solution

  • For anybody interested, I was able to download the FIPS codes using the TIGRIS package, and then assign it with various join functions.

      ##Assign FIPS by State
    ##DE_2016small$StateFIPS <- fips(DE_2016small$Residence_Addresses_State_2016)
    
      ##Assign FIPS by County
        ##Download FIPS from TidyCensus/Tigris and filter DE (Using TIGRIS) and filter
    DE_counties <- counties("DE")
    
    DE_counties <- DE_counties %>%
      select(STATEFP, COUNTYFP, NAME)
      DE_counties$geometry <- NULL
      
      ##Merge to match the County with its FIPS
    DEwFIPS <- merge(DE_counties, DE_2016county, by.x = "NAME", by.y = "County", all = TRUE)
    
      ##Concatenate FIPS codes together
    DEwFIPS$GEOID <- str_c(DE_FIPSbroad$STATEFP, DE_FIPSbroad$COUNTYFP, DE_FIPSbroad$Residence_Addresses_CensusTract_2016)
    
      ##Tidy data
    DEwFIPS <- DEwFIPS %>% relocate(GEOID, .before = NAME)````