Home > Enterprise >  Is there an R function to help turn State abbreviations into full names? Or Vice Versa?
Is there an R function to help turn State abbreviations into full names? Or Vice Versa?

Time:04-18

I have two large-ish data frames I am trying to append...

In df1, I have state codes, county codes, state names (Alabama, Alaska, etc.), county names, and years from 2010:2020.

In df2, I have county names, state abbreviations (AL, AK), and data for the year 2010 (which I am trying to merge into df1. The issue lies in that without specifying the state name and simply merging df1 and df2, some of the data which I am trying to get into df1 is duplicated due to there being some counties with the same name...hence, I am trying to also join by state to prevent this, but I have state abbreviations, and state names.

Is there any way in which I can make either the state names in df1 abbreviations, or the state names in df2 full names? Please let me know! Thank you for the help.

Edit: dput(df2)

dput(birthdata2)
structure(list(County = c("Ada County", "Adams County", "Adams County", 
"Aiken County", "Alachua County", "Alamance County", "Alameda County", 
"Albany County", "Alexandria city", "Allegan County", "Allegheny County", 
"Allen County", "Allen County", "Anchorage Borough", "Anderson County", 
"Androscoggin County", "Anne Arundel County", "Anoka County", 
"Arapahoe County", "Arlington County", "Ascension Parish", "Ashtabula County", 
"Atlantic County", "Baldwin County", "Baltimore city", "Baltimore County", 
"Barnstable County", "Bartow County", "Bay County", "Bay County", 
"Beaufort County", "Beaver County", "Bell County", "Benton County", 
"Benton County", "Bergen County", "Berkeley County", "Berkeley County", 
"Berks County", "Berkshire County", "Bernalillo County", "Berrien County", 
"Bexar County", "Bibb County", "Black Hawk County", "Blair County", 
"Blount County", "Bonneville County", "Boone County", "Boone County", 
"Bossier Parish", "Boulder County", "Brazoria County", "Brazos County", 
"Brevard County", "Bristol County", "Bronx County", "Broome County", 
"Broward County", "Brown County", "Brunswick County", "Bucks County", 
"Buncombe County", "Burlington County", "Butler County", "Butler County", 
"Butte County", "Cabarrus County", "Cache County", "Caddo Parish", 
"Calcasieu Parish", "Calhoun County", "Calhoun County", "Cambria County", 
"Camden County", "Cameron County", "Canadian County", "Canyon County", 
"Cape May County", "Carroll County", "Carroll County", "Cass County", 
"Catawba County", "Cecil County", "Centre County", "Champaign County", 
"Charles County", "Charleston County", "Charlotte County", "Chatham County", 
"Chautauqua County", "Cherokee County", "Chesapeake city", "Chester County", 
"Chesterfield County", "Chittenden County", "Citrus County", 
"Clackamas County", "Clark County", "Clark County", "Clark County", 
"Clark County", "Clarke County", "Clay County", "Clay County", 
"Clayton County", "Clermont County", "Cleveland County", "Cobb County", 
"Cochise County", "Coconino County", "Collier County", "Collin County", 
"Columbia County", "Columbiana County", "Comal County", "Comanche County", 
"Contra Costa County", "Cook County", "Coweta County", "Cowlitz County", 
"Craven County", "Cumberland County", "Cumberland County", "Cumberland County", 
"Cumberland County", "Cuyahoga County", "Dakota County", "Dallas County", 
"Dane County", "Dauphin County", "Davidson County", "Davidson County", 
"Davis County", "DeKalb County", "DeKalb County", "Delaware County", 
"Delaware County", "Delaware County", "Denton County", "Denver County", 
"Deschutes County", "DeSoto County", "District of Columbia", 
"Dona Ana County", "Dorchester County", "Douglas County", "Douglas County", 
"Douglas County", "Douglas County", "Douglas County", "DuPage County", 
"Durham County", "Dutchess County", "Duval County", "East Baton Rouge Parish", 
"Eaton County", "Ector County", "El Dorado County", "El Paso County", 
"El Paso County", "Elkhart County", "Ellis County", "Erie County", 
"Erie County", "Escambia County", "Essex County", "Essex County", 
"Etowah County", "Fairfax County", "Fairfield County", "Fairfield County", 
"Faulkner County", "Fayette County", "Fayette County", "Fayette County", 
"Florence County", "Fond du Lac County", "Forsyth County", "Forsyth County", 
"Fort Bend County", "Franklin County", "Franklin County", "Franklin County", 
"Frederick County", "Fresno County", "Fulton County", "Galveston County", 
"Gaston County", "Genesee County", "Gloucester County", "Grayson County", 
"Greene County", "Greene County", "Greenville County", "Gregg County", 
"Guadalupe County", "Guilford County", "Gwinnett County", "Hall County", 
"Hamilton County", "Hamilton County", "Hamilton County", "Hampden County", 
"Hampshire County", "Hampton city", "Hardin County", "Harford County", 
"Harnett County", "Harris County", "Harrison County", "Hartford County", 
"Hawaii County", "Hays County", "Henderson County", "Hendricks County", 
"Hennepin County", "Henrico County", "Henry County", "Hernando County", 
"Hidalgo County", "Hillsborough County", "Hillsborough County", 
"Hinds County", "Honolulu County", "Horry County", "Houston County", 
"Houston County", "Howard County", "Hudson County", "Humboldt County", 
"Hunterdon County", "Imperial County", "Indian River County", 
"Ingham County", "Iredell County", "Jackson County", "Jackson County", 
"Jackson County", "Jackson County", "Jasper County", "Jefferson County", 
"Jefferson County", "Jefferson County", "Jefferson County", "Jefferson County", 
"Jefferson County", "Jefferson Parish", "Johnson County", "Johnson County", 
"Johnson County", "Johnson County", "Johnston County", "Kalamazoo County", 
"Kanawha County", "Kane County", "Kankakee County", "Kaufman County", 
"Kendall County", "Kennebec County", "Kenosha County", "Kent County", 
"Kent County", "Kent County", "Kenton County", "Kern County", 
"King County", "Kings County", "Kings County", "Kitsap County", 
"Knox County", "Kootenai County", "La Crosse County", "La Porte County", 
"Lackawanna County", "Lafayette Parish", "Lake County", "Lake County", 
"Lake County", "Lake County", "Lancaster County", "Lancaster County", 
"Lane County", "Larimer County", "LaSalle County", "Lebanon County", 
"Lee County", "Lee County", "Lehigh County", "Leon County", "Lexington County", 
"Licking County", "Linn County", "Linn County", "Litchfield County", 
"Livingston County", "Livingston Parish", "Lorain County", "Los Angeles County", 
"Loudoun County", "Lowndes County", "Lubbock County", "Lucas County", 
"Luzerne County", "Lycoming County", "Macomb County", "Macon County", 
"Madera County", "Madison County", "Madison County", "Madison County", 
"Mahoning County", "Manatee County", "Marathon County", "Maricopa County", 
"Marin County", "Marion County", "Marion County", "Marion County", 
"Martin County", "Maui County", "McHenry County", "McLean County", 
"McLennan County", "Mecklenburg County", "Medina County", "Merced County", 
"Mercer County", "Mercer County", "Merrimack County", "Mesa County", 
"Miami County", "Miami-Dade County", "Middlesex County", "Middlesex County", 
"Middlesex County", "Midland County", "Milwaukee County", "Minnehaha County", 
"Missoula County", "Mobile County", "Mohave County", "Monmouth County", 
"Monroe County", "Monroe County", "Monroe County", "Monroe County", 
"Monterey County", "Montgomery County", "Montgomery County", 
"Montgomery County", "Montgomery County", "Montgomery County", 
"Montgomery County", "Morgan County", "Morris County", "Multnomah County", 
"Muscogee County", "Muskegon County", "Napa County", "Nassau County", 
"Navajo County", "New Castle County", "New Hanover County", "New Haven County", 
"New London County", "New York County", "Newport News city", 
"Niagara County", "Norfolk city", "Norfolk County", "Northampton County", 
"Nueces County", "Oakland County", "Ocean County", "Okaloosa County", 
"Oklahoma County", "Olmsted County", "Oneida County", "Onondaga County", 
"Onslow County", "Ontario County", "Orange County", "Orange County", 
"Orange County", "Orange County", "Orleans Parish", "Osceola County", 
"Oswego County", "Ottawa County", "Ouachita Parish", "Outagamie County", 
"Palm Beach County", "Parker County", "Pasco County", "Passaic County", 
"Paulding County", "Pennington County", "Penobscot County", "Peoria County", 
"Philadelphia County", "Pickens County", "Pierce County", "Pima County", 
"Pinal County", "Pinellas County", "Pitt County", "Placer County", 
"Plymouth County", "Polk County", "Polk County", "Portage County", 
"Porter County", "Portsmouth city", "Potter County", "Prince George's County", 
"Prince William County", "Providence County", "Pueblo County", 
"Pulaski County", "Queens County", "Racine County", "Ramsey County", 
"Randall County", "Randolph County", "Rankin County", "Rapides Parish", 
"Rensselaer County", "Richland County", "Richland County", "Richmond city", 
"Richmond County", "Richmond County", "Riverside County", "Robeson County", 
"Rock County", "Rock Island County", "Rockingham County", "Rockland County", 
"Rowan County", "Rutherford County", "Sacramento County", "Saginaw County", 
"Saline County", "Salt Lake County", "San Bernardino County", 
"San Diego County", "San Francisco County", "San Joaquin County", 
"San Juan County", "San Luis Obispo County", "San Mateo County", 
"Sandoval County", "Sangamon County", "Santa Barbara County", 
"Santa Clara County", "Santa Cruz County", "Santa Fe County", 
"Santa Rosa County", "Sarasota County", "Saratoga County", "Sarpy County", 
"Schenectady County", "Schuylkill County", "Scott County", "Scott County", 
"Sebastian County", "Sedgwick County", "Seminole County", "Shasta County", 
"Shawnee County", "Sheboygan County", "Shelby County", "Shelby County", 
"Skagit County", "Smith County", "Snohomish County", "Solano County", 
"Somerset County", "Sonoma County", "Spartanburg County", "Spokane County", 
"Spotsylvania County", "St. Charles County", "St. Clair County", 
"St. Clair County", "St. Johns County", "St. Joseph County", 
"St. Lawrence County", "St. Louis city", "St. Louis County", 
"St. Louis County", "St. Lucie County", "St. Mary's County", 
"St. Tammany Parish", "Stafford County", "Stanislaus County", 
"Stark County", "Stearns County", "Strafford County", "Suffolk County", 
"Suffolk County", "Sullivan County", "Summit County", "Sumner County", 
"Sumter County", "Sussex County", "Sussex County", "Tangipahoa Parish", 
"Tarrant County", "Taylor County", "Tazewell County", "Terrebonne Parish", 
"Thurston County", "Tippecanoe County", "Tolland County", "Tom Green County", 
"Tompkins County", "Travis County", "Trumbull County", "Tulare County", 
"Tulsa County", "Tuscaloosa County", "Ulster County", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Unidentified Counties", 
"Unidentified Counties", "Unidentified Counties", "Union County", 
"Union County", "Utah County", "Vanderburgh County", "Ventura County", 
"Vigo County", "Virginia Beach city", "Volusia County", "Wake County", 
"Walworth County", "Warren County", "Warren County", "Warren County", 
"Washington County", "Washington County", "Washington County", 
"Washington County", "Washington County", "Washington County", 
"Washington County", "Washington County", "Washington County", 
"Washoe County", "Washtenaw County", "Waukesha County", "Wayne County", 
"Wayne County", "Wayne County", "Webb County", "Weber County", 
"Weld County", "Westchester County", "Westmoreland County", "Whatcom County", 
"Whitfield County", "Wichita County", "Will County", "Williamson County", 
"Williamson County", "Wilson County", "Windham County", "Winnebago County", 
"Winnebago County", "Wood County", "Woodbury County", "Worcester County", 
"Wright County", "Wyandotte County", "Yakima County", "Yavapai County", 
"Yellowstone County", "Yolo County", "York County", "York County", 
"York County", "Yuma County"), State = c(" ID", " CO", " PA", 
" SC", " FL", " NC", " CA", " NY", " VA", " MI", " PA", " IN", 
" OH", " AK", " SC", " ME", " MD", " MN", " CO", " VA", " LA", 
" OH", " NJ", " AL", " MD", " MD", " MA", " GA", " FL", " MI", 
" SC", " PA", " TX", " AR", " WA", " NJ", " SC", " WV", " PA", 
" MA", " NM", " MI", " TX", " GA", " IA", " PA", " TN", " ID", 
" KY", " MO", " LA", " CO", " TX", " TX", " FL", " MA", " NY", 
" NY", " FL", " WI", " NC", " PA", " NC", " NJ", " OH", " PA", 
" CA", " NC", " UT", " LA", " LA", " AL", " MI", " PA", " NJ", 
" TX", " OK", " ID", " NJ", " GA", " MD", " ND", " NC", " MD", 
" PA", " IL", " MD", " SC", " FL", " GA", " NY", " GA", " VA", 
" PA", " VA", " VT", " FL", " OR", " IN", " NV", " OH", " WA", 
" GA", " FL", " MO", " GA", " OH", " OK", " GA", " AZ", " AZ", 
" FL", " TX", " GA", " OH", " TX", " OK", " CA", " IL", " GA", 
" WA", " NC", " ME", " NC", " NJ", " PA", " OH", " MN", " TX", 
" WI", " PA", " NC", " TN", " UT", " GA", " IL", " IN", " OH", 
" PA", " TX", " CO", " OR", " MS", " DC", " NM", " SC", " CO", 
" GA", " KS", " NE", " OR", " IL", " NC", " NY", " FL", " LA", 
" MI", " TX", " CA", " CO", " TX", " IN", " TX", " NY", " PA", 
" FL", " MA", " NJ", " AL", " VA", " CT", " OH", " AR", " GA", 
" KY", " PA", " SC", " WI", " GA", " NC", " TX", " MO", " OH", 
" PA", " MD", " CA", " GA", " TX", " NC", " MI", " NJ", " TX", 
" MO", " OH", " SC", " TX", " TX", " NC", " GA", " GA", " IN", 
" OH", " TN", " MA", " MA", " VA", " KY", " MD", " NC", " TX", 
" MS", " CT", " HI", " TX", " NC", " IN", " MN", " VA", " GA", 
" FL", " TX", " FL", " NH", " MS", " HI", " SC", " AL", " GA", 
" MD", " NJ", " CA", " NJ", " CA", " FL", " MI", " NC", " MI", 
" MO", " MS", " OR", " MO", " AL", " CO", " KY", " MO", " NY", 
" TX", " LA", " IA", " IN", " KS", " TX", " NC", " MI", " WV", 
" IL", " IL", " TX", " IL", " ME", " WI", " DE", " MI", " RI", 
" KY", " CA", " WA", " CA", " NY", " WA", " TN", " ID", " WI", 
" IN", " PA", " LA", " FL", " IL", " IN", " OH", " NE", " PA", 
" OR", " CO", " IL", " PA", " AL", " FL", " PA", " FL", " SC", 
" OH", " IA", " OR", " CT", " MI", " LA", " OH", " CA", " VA", 
" GA", " TX", " OH", " PA", " PA", " MI", " IL", " CA", " AL", 
" IL", " IN", " OH", " FL", " WI", " AZ", " CA", " FL", " IN", 
" OR", " FL", " HI", " IL", " IL", " TX", " NC", " OH", " CA", 
" NJ", " PA", " NH", " CO", " OH", " FL", " CT", " MA", " NJ", 
" TX", " WI", " SD", " MT", " AL", " AZ", " NJ", " IN", " MI", 
" NY", " PA", " CA", " AL", " MD", " OH", " PA", " TN", " TX", 
" AL", " NJ", " OR", " GA", " MI", " CA", " NY", " AZ", " DE", 
" NC", " CT", " CT", " NY", " VA", " NY", " VA", " MA", " PA", 
" TX", " MI", " NJ", " FL", " OK", " MN", " NY", " NY", " NC", 
" NY", " CA", " FL", " NC", " NY", " LA", " FL", " NY", " MI", 
" LA", " WI", " FL", " TX", " FL", " NJ", " GA", " SD", " ME", 
" IL", " PA", " SC", " WA", " AZ", " AZ", " FL", " NC", " CA", 
" MA", " FL", " IA", " OH", " IN", " VA", " TX", " MD", " VA", 
" RI", " CO", " AR", " NY", " WI", " MN", " TX", " NC", " MS", 
" LA", " NY", " OH", " SC", " VA", " GA", " NY", " CA", " NC", 
" WI", " IL", " NH", " NY", " NC", " TN", " CA", " MI", " AR", 
" UT", " CA", " CA", " CA", " CA", " NM", " CA", " CA", " NM", 
" IL", " CA", " CA", " CA", " NM", " FL", " FL", " NY", " NE", 
" NY", " PA", " IA", " MN", " AR", " KS", " FL", " CA", " KS", 
" WI", " AL", " TN", " WA", " TX", " WA", " CA", " NJ", " CA", 
" SC", " WA", " VA", " MO", " IL", " MI", " FL", " IN", " NY", 
" MO", " MN", " MO", " FL", " MD", " LA", " VA", " CA", " OH", 
" MN", " NH", " MA", " NY", " TN", " OH", " TN", " SC", " DE", 
" NJ", " LA", " TX", " TX", " IL", " LA", " WA", " IN", " CT", 
" TX", " NY", " TX", " OH", " CA", " OK", " AL", " NY", " AK", 
" AL", " AR", " AZ", " CA", " CO", " FL", " GA", " HI", " IA", 
" ID", " IL", " IN", " KS", " KY", " LA", " MA", " MD", " ME", 
" MI", " MN", " MO", " MS", " MT", " NC", " ND", " NE", " NH", 
" NJ", " NM", " NV", " NY", " OH", " OK", " OR", " PA", " RI", 
" SC", " SD", " TN", " TX", " UT", " VA", " VT", " WA", " WI", 
" WV", " WY", " NC", " NJ", " UT", " IN", " CA", " IN", " VA", 
" FL", " NC", " WI", " KY", " NJ", " OH", " AR", " MD", " MN", 
" OR", " PA", " RI", " TN", " UT", " WI", " NV", " MI", " WI", 
" MI", " NC", " OH", " TX", " UT", " CO", " NY", " PA", " WA", 
" GA", " TX", " IL", " TN", " TX", " TN", " CT", " IL", " WI", 
" OH", " IA", " MA", " MN", " KS", " WA", " AZ", " MT", " CA", 
" ME", " PA", " SC", " AZ"), x = c(358, 549, NA, 149, 219, 178, 
1367, 257, 194, 76, 1032, 432, 114, 278, 177, 69, 574, 265, 701, 
208, NA, 77, 267, 176, 1044, 826, 107, NA, 179, 78, 193, 119, 
538, 246, 162, 765, 202, NA, 409, 84, 778, 177, 2441, 321, 123, 
101, 91, NA, NA, 173, NA, 228, 390, 192, 372, 472, 2190, 173, 
1930, 209, NA, 469, 194, 379, 340, 112, 146, 225, NA, 573, 258, 
103, 87, 119, 531, 622, NA, 187, 56, NA, 78, 141, 155, NA, 76, 
198, 164, 419, 74, 378, 99, 240, 222, 371, 298, 95, 77, 243, 
NA, 2217, 101, 340, 116, 170, 206, 437, 202, 228, 790, 140, 129, 
248, 844, NA, 73, NA, 155, 851, 6598, NA, NA, NA, 164, 575, 219, 
152, 1565, 319, 3336, 388, 335, 179, 825, 374, 1104, NA, 97, 
131, 537, 621, 915, 83, 141, 934, 240, NA, 353, NA, NA, 680, 
76, 789, 406, 187, 1171, 732, 72, 249, 115, 857, 1265, 235, 132, 
788, 305, 412, 569, 1087, 93, 1060, 776, 87, NA, NA, 319, 137, 
220, NA, NA, 477, 701, NA, 1676, 138, 237, 1266, 1467, 338, 248, 
523, 253, 109, 264, 137, 501, 167, NA, 562, 863, 221, 256, 1043, 
436, 451, 74, 177, NA, 202, NA, 6004, 295, 835, 180, NA, NA, 
111, 1167, 352, 221, 110, 1284, 1484, 317, 553, 1181, 311, NA, 
185, 259, 842, 76, 70, 182, 89, 257, 148, 156, 795, 159, 150, 
109, 998, 486, 972, 179, 130, 368, 551, 114, 100, 463, 169, 167, 
245, 182, 518, 99, NA, NA, 70, 155, 173, 654, 102, 199, 1010, 
1602, 158, 3530, 189, 449, 105, 65, 123, 177, 289, 230, 644, 
618, 147, 302, 517, 215, 257, 73, 97, 117, 488, 333, 267, 274, 
157, 178, 79, 118, 108, NA, 231, 9714, 345, NA, 399, 562, 272, 
73, 766, 118, 164, 446, 255, 112, 242, 262, 68, 3848, 141, 267, 
1403, 279, 77, 144, 263, 155, 255, 1340, 87, 285, 403, 72, 93, 
110, NA, 2862, 72, 1389, 853, 167, 1329, 200, NA, 694, 160, 472, 
79, 99, 722, 145, 370, 348, 1026, 634, 612, 257, 474, 134, 355, 
629, 348, 164, 82, 1137, NA, 631, 176, 833, 190, 1737, 266, 166, 
379, 479, 281, 377, 1045, 530, 185, 1095, 115, 186, 429, 304, 
76, 2459, 1373, 108, 345, 571, 301, 95, 183, 253, 157, 1238, 
NA, 396, 590, NA, NA, 78, 198, 2523, 65, 633, 853, 328, 771, 
197, 206, 369, 601, 504, 119, 74, 155, 175, 1243, 497, 602, 174, 
537, 2557, 174, 516, 108, 131, 225, 156, 111, 109, 566, 336, 
336, 466, 1989, 235, 165, 143, 170, 309, 150, 307, 1419, 202, 
NA, 1323, 2224, 2895, 622, 746, 109, 121, 655, NA, 237, 325, 
1648, 178, 135, 117, 213, 144, 147, 153, 100, 142, NA, 137, 688, 
343, 104, 177, 59, 203, 1526, 64, 238, 536, 320, 279, 307, 390, 
387, NA, 326, 320, 133, 106, 294, 70, 597, 118, 1005, 271, NA, 
204, NA, 464, 343, 93, 81, 860, 1301, 151, 552, 129, 176, 201, 
103, 202, 2341, 146, 92, 173, 149, 135, 68, 113, NA, 1250, 167, 
508, 809, 283, 115, 369, 2516, 2223, 401, 599, 849, 1237, 5841, 
57, 1503, 911, 1908, 2311, 1294, 3545, 2704, 51, 625, 268, 1538, 
1605, 2599, 3450, 744, 3550, 464, 696, 201, 45, 1143, 297, 1472, 
2483, 2155, 643, 1334, 61, 1797, 604, 2718, 5530, 868, 3426, 
276, 944, 1483, 1696, 673, 202, 570, 752, 184, 686, 71, 485, 
389, 1018, NA, NA, 53, 178, 232, 155, 157, 390, 163, 56, 106, 
NA, 59, 448, 318, 284, 2572, 121, 100, 398, 328, 267, 965, 251, 
103, NA, 150, 643, 125, 458, NA, 68, 321, 142, 75, 106, 653, 
NA, 239, 292, 110, 141, 125, 113, 425, 256, 197), year = c(2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010
)), row.names = c(NA, -628L), class = c("tbl_df", "tbl", "data.frame"
))

CodePudding user response:

Here's one way you could turn state abbreviations into state names using R's built in state vectors:

a <- c('NJ', 'MA', 'FL')
state.name[sapply(a, \(x) which(x == state.abb))]

[1] "New Jersey"    "Massachusetts" "Florida" 

Applying this to birthdata2, we might create a column called state_name. But first we need to trim whitespace from the State column:

birthdata2$State <- trimws(birthdata2$State)
birthdata2$state_name <- state.name[sapply(birthdata2$State, \(x) which(x == state.abb)[1])]

   County          State     x  year state_name    
   <chr>           <chr> <dbl> <dbl> <chr>         
 1 Ada County      ID      358  2010 Idaho         
 2 Adams County    CO      549  2010 Colorado      
 3 Adams County    PA       NA  2010 Pennsylvania  
 4 Aiken County    SC      149  2010 South Carolina
 5 Alachua County  FL      219  2010 Florida       
 6 Alamance County NC      178  2010 North Carolina
 7 Alameda County  CA     1367  2010 California    
 8 Albany County   NY      257  2010 New York      
 9 Alexandria city VA      194  2010 Virginia      
10 Allegan County  MI       76  2010 Michigan      
# … with 618 more rows
  •  Tags:  
  • r
  • Related