Search code examples
rfips

From state and county names to fips in R


I have the following data frame in R. I would like to get fips from this dataset. I tried to use fips function in usmap (https://rdrr.io/cran/usmap/man/fips.html). But I could not get fips from this function because I need to enclose double quote. Then, I tried to use paste0(""", df$state, """), but I could not get it. Is there any efficient ways to get fips?

> df1
                   state           county
1             california             napa
2                florida       palm beach
3                florida          collier
4                florida            duval

UPDATE

I can get "\"california\"" by using dQuote. Thanks. After the conversion of each column, I tried the followings. How do I deal with this issue?

> df1$state <- dQuote(df1$state, FALSE)
> df1$county <- dQuote(df1$county, FALSE)
> fips(state = df1$state, county = df1$county)
Error in fips(state = df1$state, county = df1$county) : 
  `county` parameter cannot be used with multiple states.

> fips(state = df1$state[1], county = df1$county[1])
Error in fips(state = df1$state[1], county = df1$county[1]) : 
  "napa" is not a valid county in "california".

> fips(state = "california", county = "napa")
[1] "06055"

Solution

  • We can split the dataset by state and apply the fips

    library(usmap)
    lapply(split(df1, df1$state), function(x)
          fips(state = x$state[1], county = x$county))
    #$california
    #[1] "06055"
    
    #$florida
    #[1] "12099" "12021" "12031"
    

    Or with Map

    lst1 <- split(df1$county, df1$state)
    Map(fips, lst1, state = names(lst1))
    #$california
    #[1] "06055"
    
    #$florida
    #[1] "12099" "12021" "12031"
    

    Or with tidyverse

    library(dplyr)
    library(tidyr)
    df1 %>% 
        group_by(state) %>% 
        summarise(new = list(fips(state = first(state), county = county))) %>%
        unnest(c(new))
    # A tibble: 4 x 2
    #  state      new  
    #  <chr>      <chr>
    #1 california 06055
    #2 florida    12099
    #3 florida    12021
    #4 florida    12031
    

    data

    df1 <- structure(list(state = c("california", "florida", "florida", 
    "florida"), county = c("napa", "palm beach", "collier", "duval"
    )), class = "data.frame", row.names = c("1", "2", "3", "4"))