Having challenges merging shape file from iowa counties to my data

I downloaded the shapefile data from US census bureau, class is both dataframe, am trying to merge subregion in both, so I have changed NAME in the previous counties which is reflecting from the names lines of code. However, merging would not work, I have explored all possible merging function.

class(iowa_map)
class(data)

Check column names used in left_join

names(iowa_map)
names(data)

Check for missing values in merging columns

sum(is.na(iowa_counties$NAME))
sum(is.na(data$subregion))

iowa_map_sf <- st_as_sf(iowa_map)

merged_data <- left_join(iowa_map_sf, data, by = c("subregion" = "subregion"))
#merged_data <- merge(iowa_map_sf, data, by.x = "subregion", by.y = "subregion", all.x = TRUE)
merged_data

Here's how to get the county geometry

library(tidycensus)
iowa_co <- get_acs(geography = "county", variables = "B19013_001",
                state = "IA", geometry = TRUE, year = 2020)
#> Getting data from the 2016-2020 5-year ACS
#> Warning: • You have not set a Census API key. Users without a key are limited to 500
#> queries per day and may experience performance limitations.
#> ℹ For best results, get a Census API key at
#> http://api.census.gov/data/key_signup.html and then supply the key to the
#> `census_api_key()` function to use it throughout your tidycensus session.
#> This warning is displayed once per session.
#> Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
#>   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |==============================                                        |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |=================================                                     |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |===================================                                   |  51%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |=========================================                             |  58%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |====================================================                  |  74%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |===============================================================       |  89%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
head(iowa_co)
#> Simple feature collection with 6 features and 5 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -96.63862 ymin: 41.7277 xmax: -90.14006 ymax: 43.50047
#> Geodetic CRS:  NAD83
#>   GEOID                  NAME   variable estimate  moe
#> 1 19143  Osceola County, Iowa B19013_001    61167 5550
#> 2 19075   Grundy County, Iowa B19013_001    71760 2530
#> 3 19041     Clay County, Iowa B19013_001    51259 4208
#> 4 19081  Hancock County, Iowa B19013_001    61957 2562
#> 5 19045  Clinton County, Iowa B19013_001    52221 1825
#> 6 19149 Plymouth County, Iowa B19013_001    71147 3210
#>                         geometry
#> 1 MULTIPOLYGON (((-95.86127 4...
#> 2 MULTIPOLYGON (((-93.02679 4...
#> 3 MULTIPOLYGON (((-95.38863 4...
#> 4 MULTIPOLYGON (((-93.97129 4...
#> 5 MULTIPOLYGON (((-90.89894 4...
#> 6 MULTIPOLYGON (((-96.63836 4...

Created on 2023-03-15 with reprex v2.0.2

I don't know what is meant by "subregion" or understand the objective of combining with counties. Are sub-regions groups of counties or sections of counties? Is each county in only one sub-region or each sub-region in only one county? Consider a reprex. See the FAQ.

I truly appreciate the help! But just after I posted this, I realized after I have changed both counties in my data and the map file to lower-case, it was still not getting that right for whatever reason, that makes joining eventually impossible. I was just wondering how would have spent so much time on such trivial error.

1 Like

I’ve learned that punctuation is always the first place to look, ahead even of rebooting.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.