How do I use the usmap package to convert the state variable to the FIPS code?

862 views Asked by At

I also want to change the state column to be in terms of the FIPS code. Just not sure what parameters to use and how to do this since I am new to R.

Here are the parameters given by R:

plot_usmap(regions = c("states", "state", "counties", "county"),
  include = c(), data = data.frame(), values = "values",
  theme = theme_map(), lines = "black", labels = FALSE,
  label_color = "black")
1

There are 1 answers

2
C. Braun On

It is unclear exactly what you are trying to achieve without an example, but here is how I was able to convert a column state in a data.frame from the abbreviation to the FIPS code:

> library(usmap)
> df <- statepop[1:5, -1]
> names(df)[1] <- 'state'
> df
# A tibble: 5 x 3
  state full       pop_2015
  <chr> <chr>         <dbl>
1 AL    Alabama     4858979
2 AK    Alaska       738432
3 AZ    Arizona     6828065
4 AR    Arkansas    2978204
5 CA    California 39144818
> df$fips <- fips(df$state)
> df
# A tibble: 5 x 4
  state full       pop_2015 fips 
  <chr> <chr>         <dbl> <chr>
1 AL    Alabama     4858979 01   
2 AK    Alaska       738432 02   
3 AZ    Arizona     6828065 04   
4 AR    Arkansas    2978204 05   
5 CA    California 39144818 06