Skip to content

Instantly share code, notes, and snippets.

@dreidpath
Last active June 3, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreidpath/22ea8d14552c2aa73de2 to your computer and use it in GitHub Desktop.
Save dreidpath/22ea8d14552c2aa73de2 to your computer and use it in GitHub Desktop.
An R script to validate Malaysian National Registration Identity Card numbers
#####################################################################
# A series of functions to manage Malaysian National Registration #
# Identity Card Number (NRIC) data. The NRIC was a form #
# YYMMDD-PB-XXXG[*], where YYMMDD are six digits representing the #
# date of birth/registration, PB is a two digit code representing #
# the place of birth XXX is a random digit and G is also a random #
# digit, the if G is even the NRIC is for a female and if it is odd #
# it is for a male. #
#-------------------------------------------------------------------#
# The functions: #
# is.nric() returns a boolean; T if the string is formatted as an #
# NRIC, otherwise F. #
# sexFromNRIC() returns the sex associated with the NRIC #
# dobFromNRIC() returns the date of birth. #
# #
# Author: Daniel D Reidpath #
# Date: 31 October, 2015 #
#####################################################################
#
dobFromNRIC <- function(nric, as.date=T){
# nric as an appropriately formatted NRIC YYMMDD-PB-XXXG
# as.date ensure that the returned values are eighter as.Date or as
# strings.
#
dob <- substr(nric, 1, 6)
yr <- as.integer(substr(dob, 1,2))
dob <- sapply(yr, function(century, dobstr){
if(century>=16){
dobstr <- paste("19", substr(dobstr, 1,2), "-", substr(dobstr, 3,4), "-", substr(dobstr, 5,6), sep='')
} else {
dobstr <- paste("20", substr(dobstr, 1,2), "-", substr(dobstr, 3,4), "-", substr(dobstr, 5,6), sep='')
}
return(dobstr)
}, dob)
if(as.date==T){
return( as.Date(as.vector(dob)[1:length(nric)]) )
}
if(as.date==F){
return(as.vector(dob)[1:length(nric)])
}
else{
stop( "as.date must be a logical")
}
}
is.nric <- function(nric){
# Boolean function checks that nric is formatted correctly
nric_format <- "[0-9]{2}[0-9]{1}[012]{1}[0123]{1}[0-9]{1}-[1-9]{2}-[0-9]{4}"
grepl(nric_format, nric)
}
sexFromNRIC <- function(nric){
# nric as an appropriately formatted NRIC YYMMDD-PB-XXXG
# returns the gender associated with the NRIC: 1=Female, 2=Male
sex <- sapply(nric, function(x){
if(!is.nric(x)){
sex <- NA
} else {
last_digit <- substr(x, nchar(x), nchar(x))
last_digit <- as.integer( last_digit )
sex <- last_digit %% 2 + 1 # 1=Female, 2=Male
return(sex)
}
return(sex)
})
return(sex)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment