Skip to contents

adm2_code often refers to the codes of autonomous _Gu_s or non-autonomous _Gu_s. The head table of the data frame may contain either or both of the two types of codes. This function detects the type of the codes in the adm2_code field and returns the exact codes accordingly.

Usage

detect_adm2_type(df, year = NULL, mode = "non", adm2_code = "adm2_code")

Arguments

df

A head data frame containing the full dataset. i.e., censuskor

year

The year for which to filter the data. If not specified, the function will use the data.frame as is.

mode

A character vector of "atn" (autonomous) and "non" (non-autonomous).

adm2_code

A character vector of adm2_code field Default is "adm2_code".

Value

filtered data frame with exact codes

Examples

# Load 2020 census population
pop20 <- anycensus(year = 2020, type = "population")
#> Using character codes that are convertible to integers. Automatically converting to integers...
pop20_nonauto <- detect_adm2_type(pop20, mode = "non")
pop20_auto <- detect_adm2_type(pop20, mode = "atn")
unique(pop20_nonauto$adm2_code)
#>   [1] 37040 31220 34040 33320 37410 34030 36360 35380 31050 21080 22050 24040
#>  [13] 26040 37012 31023 23060 21050 34330 38330 31191 37360 37330 33044 34350
#>  [25] 32360 37390 32010 33020 25050 22070 22310 36310 34080 31092 33380 31101
#>  [37] 35012 11100 21030 22020 25010 24010 23020 26030 31042 11060 31080 32040
#>  [49] 11200 21060 34011 33370 11120 11090 11250 23310 36390 11230 32030 21120
#>  [61] 11160 31370 38390 38090 11180 21110 34310 31192 21310 37030 38070 35060
#>  [73] 31230 35370 33360 36350 36320 34020 37370 32400 38340 37050 31160 35020
#>  [85] 37310 31120 11170 36330 31110 11210 11050 31250 31060 24050 36060 31012
#>  [97] 37020 37100 34070 23070 38360 36400 21090 38320 36430 38380 31180 38400
#> [109] 33043 32320 32310 34360 32370 31240 36370 31210 35030 31103 31104 35350
#> [121] 32390 31011 36380 36450 35340 33030 39010 35040 32350 33390 35320 33350
#> [133] 36470 38115 38030 11010 21010 22010 25020 23010 11020 26010 11070 31022
#> [145] 31041 11140 38113 38114 23090 38080 36010 36420 35330 37090 36040 21070
#> [157] 22040 24030 26020 37011 23050 38350 35050 31130 34060 11110 33330 23320
#> [169] 31140 31200 31013 31270 32340 31070 38060 21100 32070 38370 33041 37080
#> [181] 31091 21150 29010 21020 22030 25030 24020 23080 34012 34340 11220 11130
#> [193] 39020 11080 11040 37380 38112 34050 33042 31150 36480 32060 11240 31021
#> [205] 31193 35360 36030 22060 21140 34380 32050 38050 38111 31030 38310 37320
#> [217] 31170 37420 26310 37430 36460 35310 35011 32020 11150 32380 31260 31380
#> [229] 38100 32410 37400 31280 31350 36410 37070 37350 11190 21040 33340 36440
#> [241] 37060 31014 32330 37340 21130 23040 36020 34370 11030 25040
unique(pop20_auto$adm2_code)
#>   [1] 33040 34010 37010 38110 35010 37040 31090 31220 31040 34040 33320 37410
#>  [13] 34030 36360 35380 31050 21080 22050 24040 26040 23060 21050 34330 38330
#>  [25] 37360 37330 34350 32360 37390 32010 33020 25050 22070 22310 36310 34080
#>  [37] 33380 11100 21030 22020 25010 24010 23020 26030 11060 31080 32040 11200
#>  [49] 21060 33370 11120 11090 11250 23310 36390 11230 32030 21120 11160 31370
#>  [61] 38390 38090 11180 21110 34310 21310 37030 38070 35060 31230 35370 33360
#>  [73] 36350 36320 34020 37370 32400 38340 31100 37050 31160 35020 37310 31120
#>  [85] 11170 36330 31110 11210 11050 31250 31060 24050 36060 37020 37100 34070
#>  [97] 23070 38360 36400 21090 38320 36430 38380 31180 38400 32320 32310 34360
#> [109] 32370 31240 36370 31210 35030 35350 32390 36380 36450 35340 33030 39010
#> [121] 35040 32350 33390 35320 33350 36470 38030 11010 21010 22010 25020 23010
#> [133] 11020 26010 11070 11140 23090 38080 36010 36420 35330 37090 36040 21070
#> [145] 22040 24030 26020 23050 38350 35050 31130 34060 11110 33330 23320 31140
#> [157] 31200 31270 32340 31070 38060 21100 32070 38370 37080 21150 29010 21020
#> [169] 22030 25030 24020 23080 34340 11220 11130 39020 11080 11040 37380 31020
#> [181] 34050 31150 36480 32060 11240 35360 36030 22060 31010 21140 34380 32050
#> [193] 38050 31030 38310 37320 31170 37420 26310 37430 36460 35310 32020 11150
#> [205] 32380 31260 31380 38100 32410 37400 31280 31350 36410 37070 37350 11190
#> [217] 21040 33340 36440 37060 32330 37340 21130 23040 36020 34370 31190 11030
#> [229] 25040