Create region variable from state input
state_to_region.Rd
This function works with title case names of the 50 states and D.C., as well as with all caps 2-letter abbreviations with and without periods (e.g., NJ and N.J.). Default regions are defined by the U.S. Census.
Usage
state_to_region(
data,
state.var = state,
region.var = region,
factorize = TRUE,
region.levels = c("Northeast", "Midwest", "South", "West")
)
Arguments
- data
A data frame or data frame extension (e.g. a tibble)
- state.var
Variable containing state values. Defaults to state
- region.var
Variable to be created containing regions. Defaults to region
- factorize
A logical evaluation to TRUE or FALSE indicating whether to make the region.var a factor. Defaults to TRUE
- region.levels
An optional vector of the unique values (as character strings) that region.var might have taken. Defaults to c("Northeast", "Midwest", "South", "West")
Examples
USArrests |> tibble::rownames_to_column("us_state") |>
state_to_region(us_state, us_region)
#> us_state Murder Assault UrbanPop Rape us_region
#> 1 Alabama 13.2 236 58 21.2 South
#> 2 Alaska 10.0 263 48 44.5 West
#> 3 Arizona 8.1 294 80 31.0 West
#> 4 Arkansas 8.8 190 50 19.5 South
#> 5 California 9.0 276 91 40.6 West
#> 6 Colorado 7.9 204 78 38.7 West
#> 7 Connecticut 3.3 110 77 11.1 Northeast
#> 8 Delaware 5.9 238 72 15.8 South
#> 9 Florida 15.4 335 80 31.9 South
#> 10 Georgia 17.4 211 60 25.8 South
#> 11 Hawaii 5.3 46 83 20.2 West
#> 12 Idaho 2.6 120 54 14.2 West
#> 13 Illinois 10.4 249 83 24.0 Midwest
#> 14 Indiana 7.2 113 65 21.0 Midwest
#> 15 Iowa 2.2 56 57 11.3 Midwest
#> 16 Kansas 6.0 115 66 18.0 Midwest
#> 17 Kentucky 9.7 109 52 16.3 South
#> 18 Louisiana 15.4 249 66 22.2 South
#> 19 Maine 2.1 83 51 7.8 Northeast
#> 20 Maryland 11.3 300 67 27.8 South
#> 21 Massachusetts 4.4 149 85 16.3 Northeast
#> 22 Michigan 12.1 255 74 35.1 Midwest
#> 23 Minnesota 2.7 72 66 14.9 Midwest
#> 24 Mississippi 16.1 259 44 17.1 South
#> 25 Missouri 9.0 178 70 28.2 Midwest
#> 26 Montana 6.0 109 53 16.4 West
#> 27 Nebraska 4.3 102 62 16.5 Midwest
#> 28 Nevada 12.2 252 81 46.0 West
#> 29 New Hampshire 2.1 57 56 9.5 Northeast
#> 30 New Jersey 7.4 159 89 18.8 Northeast
#> 31 New Mexico 11.4 285 70 32.1 West
#> 32 New York 11.1 254 86 26.1 Northeast
#> 33 North Carolina 13.0 337 45 16.1 South
#> 34 North Dakota 0.8 45 44 7.3 Midwest
#> 35 Ohio 7.3 120 75 21.4 Midwest
#> 36 Oklahoma 6.6 151 68 20.0 South
#> 37 Oregon 4.9 159 67 29.3 West
#> 38 Pennsylvania 6.3 106 72 14.9 Northeast
#> 39 Rhode Island 3.4 174 87 8.3 Northeast
#> 40 South Carolina 14.4 279 48 22.5 South
#> 41 South Dakota 3.8 86 45 12.8 Midwest
#> 42 Tennessee 13.2 188 59 26.9 South
#> 43 Texas 12.7 201 80 25.5 South
#> 44 Utah 3.2 120 80 22.9 West
#> 45 Vermont 2.2 48 32 11.2 Northeast
#> 46 Virginia 8.5 156 63 20.7 South
#> 47 Washington 4.0 145 73 26.2 West
#> 48 West Virginia 5.7 81 39 9.3 South
#> 49 Wisconsin 2.6 53 66 10.8 Midwest
#> 50 Wyoming 6.8 161 60 15.6 West