Welcome to R Square

用 CNID 包获取身份证基本信息

楚新元 / 2024-02-28


身份证号码包含很多信息,但是 CRAN 却没有提取身份证信息的包,真的很掉链子,因此我开发了 CNID 包1。这个包可以帮助您从中国身份证号码中获取出生日期、性别、地区,由此可以根据这些信息进一步得到对年对月的年龄、只对年的年龄、生肖,星座等信息。规则很简单,但我要考虑更多的情况,例如支持 15 位和 18 位号码,身份证号码中可能有逻辑错误的情况,比如 20220229 等,一旦有逻辑错误,一棍子打死也不合适,这让用户很懵,不知道这个身份证到底哪里有问题,因此我对于有逻辑错误的身份证号码仍然尽可能的提取出局部符合逻辑的信息,例如:一个身份证号只是日期有逻辑错误,但是并不妨碍这个身份证号地区和性别的判断。

为了实现以上功能,附带的我还编制了几个辅助函数,check_id() 用来检查身份证号是否符合逻辑,mdays() 用来计算某年某月有多少天,ydays() 用来计算某年有多少天。具体细节不再赘述,我的 CNID 包已经发布到 CRAN,欢迎使用:

检查身份证号码是否有逻辑错误

library(CNID)
id = c(
  "653127198503161793",
  "652801197305161555",
  "130206202202291545", 
  "110101841125178",
  "12345678",
  "65312a198204181793"
)
check_id(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1]  TRUE FALSE FALSE  TRUE FALSE FALSE

从身份证号得到全部信息

cnid_info(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#>                   id    id_type  check                               note valid
#> 1 653127198503161793 18位身份证   通过                                     TRUE
#> 2 652801197305161555 18位身份证 不通过                   [校验码校验失败] FALSE
#> 3 130206202202291545 18位身份证 不通过 [校验码校验失败][出生日期登记错误] FALSE
#> 4    110101841125178 15位身份证                                            TRUE
#> 5           12345678       <NA>                      [不满足15或18位要求] FALSE
#> 6 65312a198204181793       <NA>                        [包含不合规的字符] FALSE
#>                                         region gender birth_date age
#> 1           新疆维吾尔族自治区喀什地区麦盖提县     男 1985-03-16  40
#> 2 新疆维吾尔族自治区巴音郭楞蒙古自治州库尔勒市     男 1973-05-16  51
#> 3                             河北省唐山市新区     女       <NA>  NA
#> 4                                 北京市东城区     女 1984-11-25  40
#> 5                                         <NA>   <NA>       <NA>  NA
#> 6                                         <NA>   <NA>       <NA>  NA
#>   age_by_year zodiac   cstl
#> 1          40     牛 双鱼座
#> 2          52     牛 金牛座
#> 3          NA   <NA>   <NA>
#> 4          41     鼠 射手座
#> 5          NA   <NA>   <NA>
#> 6          NA   <NA>   <NA>

获得出生日期、年龄、性别等

birth_date(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] "1985-03-16" "1973-05-16" NA           "1984-11-25" NA          
#> [6] NA
age(id)  # 对年对月
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] 40 51 NA 40 NA NA
age_by_year(id)  # 只对年
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] 40 52 NA 41 NA NA
gender(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] "男" "男" "女" "女" NA   NA
region(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] "新疆维吾尔族自治区喀什地区麦盖提县"          
#> [2] "新疆维吾尔族自治区巴音郭楞蒙古自治州库尔勒市"
#> [3] "河北省唐山市新区"                            
#> [4] "北京市东城区"                                
#> [5] NA                                            
#> [6] NA
zodiac(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] "牛" "牛" NA   "鼠" NA   NA
cstl(id)
#> WARNING:The ID number '652801197305161555' is invalid. Please check it's 18th digit check code.
#> WARNING:The ID number '130206202202291545' is invalid. Please check it's 18th digit check code and date of birth.
#> WARNING:The ID number '12345678' is invalid. It must contain either 15 or 18 digits.
#> WARNING:The ID number '65312a198204181793' is invalid. It must be 15 pure digits, or 18 characters where the first 17 are digits and the last is either a digit or 'X'.
#> [1] "双鱼座" "金牛座" NA       "射手座" NA       NA

代码开源,欢迎读者使用和反馈改进意见和建议,引用请注明出处。


  1. 这个包代码写的有点糟糕需进一步优化,但是暂时不影响使用。 ↩︎