Concatenate to Congressional District code

to_cd(state, num)

Arguments

state

A vector of state names, preferably abbreviations. If it is numeric, the function will assume they are FIPS codes and translate them accordingly. If they have full names like "California" instead of "CA", it will trnslate that too.

num

A vector of district codes

Examples

library(dplyr)

 to_cd(c("AL", "AK"), c("5", "AL"))
#> [1] "AL-05" "AK-01"
 to_cd(c(1, 2), c("5", "AL"))
#> [1] "AL-05" "AK-01"
 to_cd(c("Alabama", "Alaska"), c("5", "AL"))
#> [1] "AL-05" "AK-01"

 transmute(cc18_samp,
           inputstate,
           cdid115,
           cd = to_cd(inputstate, cdid115))
#> Warning: There was 1 warning in `transmute()`.
#>  In argument: `cd = to_cd(inputstate, cdid115)`.
#> Caused by warning:
#> ! Unreplaced values treated as NA as `.x` is not compatible.
#> Please specify replacements exhaustively or supply `.default`.
#> # A tibble: 1,000 × 3
#>    inputstate          cdid115 cd   
#>    <dbl+lbl>           <chr>   <chr>
#>  1  6 [California]     13      CA-13
#>  2 27 [Minnesota]      7       MN-07
#>  3 37 [North Carolina] 2       NC-02
#>  4 26 [Michigan]       2       MI-02
#>  5 51 [Virginia]       7       VA-07
#>  6 36 [New York]       19      NY-19
#>  7 54 [West Virginia]  2       WV-02
#>  8 36 [New York]       15      NY-15
#>  9 48 [Texas]          3       TX-03
#> 10 25 [Massachusetts]  6       MA-06
#> # ℹ 990 more rows