15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。
18位身份证号码:第7、8、9、10位为出生年份(四位数),第11、第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男,偶数为女。
#出生日期处理函数
afunc<-function(id){
if(nchar(id)==15)
paste('19',substr(id,7,12),sep="")
else
substr(id,7,14)
}
#性别处理函数
bfunc<-function(id){
if(nchar(id)==15)
ifelse(as.numeric(substr(id,15,15)) %% 2 ==1,'M','F')
else
ifelse(as.numeric(substr(id,17,17)) %% 2 ==1,'M','F')
}
(1)测试数据
330622810725323
110221290815224
431021197306142736
431128197009055759
440700197510019150
360731196804216811
150123195103126841
222424195110306886
61102319591227666X
141182195505236567
220182196410190862
14062219620604034X
341124196902230765
(2)测试
#测试数据
ids<-c("330622810725323","110221290815224","431021197306142736","431128197009055759","440700197510019150","360731196804216811","150123195103126841","222424195110306886","61102319591227666X","141182195505236567","220182196410190862","14062219620604034X","341124196902230765")
#转换为矩阵
m.testdat<-matrix(ids,ncol=1)
#提取出身日期和性别
birthday<-apply(m.testdat,1,afunc)
gendor<-apply(m.testdat,1,bfunc)
result<-data.frame(ids,birthday,gendor)
print(result)
输出结果:
ids birthday gendor
1 330622810725323 19810725 M
2 110221290815224 19290815 F
3 431021197306142736 19730614 M
4 431128197009055759 19700905 M
5 440700197510019150 19751001 M
6 360731196804216811 19680421 M
7 150123195103126841 19510312 F
8 222424195110306886 19511030 F
9 61102319591227666X 19591227 F
10 141182195505236567 19550523 F
11 220182196410190862 19641019 F
12 14062219620604034X 19620604 F
13 341124196902230765 19690223 F