2018-10-02 21:07:43 +02:00
|
|
|
#' Extracting date of birth from CPR
|
|
|
|
#'
|
|
|
|
#' For easy calculation.
|
|
|
|
#' @param cpr cpr-numbers in format ddmmyy-xxxx.
|
|
|
|
#' @keywords cpr
|
|
|
|
#' @export
|
|
|
|
#' @examples
|
2018-10-04 17:04:15 +02:00
|
|
|
#' dob_extract_cpr("231045-0637")
|
2018-10-02 21:07:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
dob_extract_cpr<-function(cpr)
|
|
|
|
## Input as cpr-numbers in format ddmmyy-xxxx
|
|
|
|
## Build upon data from this document: https://cpr.dk/media/167692/personnummeret%20i%20cpr.pdf
|
|
|
|
## example vector: fsd<-c("010190-2000", "010115-4000", "010189-6000","010189-3000","010150-6000","010150-4000")
|
2022-08-24 14:37:30 +02:00
|
|
|
## cpr <- "231045-0637"
|
|
|
|
## cpr <- "2310450637"
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
2022-08-24 14:37:30 +02:00
|
|
|
|
|
|
|
if (substr(cpr,7,7)=="-"){ # test if input is ddmmyy-xxxx, standard format
|
|
|
|
message("Input er i formatet ddmmyy-xxxx")
|
|
|
|
cpr_std<-TRUE
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any(substr(cpr,7,7)%in%c(0:9))){
|
|
|
|
message("Input er i formatet ddmmyyxxxx") # test if input is ddmmyyxxxx
|
|
|
|
cpr_std<-FALSE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
dobs<-c()
|
2018-10-04 17:04:15 +02:00
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
a00<-as.numeric(c(0:99))
|
|
|
|
a36<-as.numeric(c(0:36))
|
|
|
|
a57<-as.numeric(c(0:57))
|
|
|
|
b00<-as.numeric(c(0,1,2,3))
|
|
|
|
b36<-as.numeric(c(4,9))
|
|
|
|
b57<-as.numeric(c(5,6,7,8))
|
2018-10-04 17:04:15 +02:00
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
for (x in cpr)
|
|
|
|
{
|
|
|
|
p56<-as.numeric(substr(x,5,6))
|
2022-08-24 14:37:30 +02:00
|
|
|
|
|
|
|
if (cpr_std){p8<-as.numeric(substr(x,8,8))} else {p8<-as.numeric(substr(x,9,9))}
|
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
birth<-as.Date(substr(x,1,6),format="%d%m%y")
|
2018-10-04 17:04:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (((p56%in%a00)&&(p8%in%b00)))
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
|
|
|
dob<-as.Date(format(birth, format="19%y%m%d"), format="%Y%m%d")
|
|
|
|
}
|
2018-10-04 17:04:15 +02:00
|
|
|
else if (((p56%in%a36)&&(p8%in%b36)))
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
|
|
|
dob<-as.Date(format(birth, format="20%y%m%d"), format="%Y%m%d")
|
|
|
|
}
|
2018-10-04 17:04:15 +02:00
|
|
|
else if ((!(p56%in%a36)&&(p8%in%b36)))
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
|
|
|
dob<-as.Date(format(birth, format="19%y%m%d"), format="%Y%m%d")
|
|
|
|
}
|
2018-10-04 17:04:15 +02:00
|
|
|
else if (((p56%in%a57)&&(p8%in%b57)))
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
|
|
|
dob<-as.Date(format(birth, format="20%y%m%d"), format="%Y%m%d")
|
|
|
|
}
|
2018-10-04 17:04:15 +02:00
|
|
|
else if ((!(p56%in%a57)&&(p8%in%b57)))
|
2018-10-02 21:07:43 +02:00
|
|
|
{
|
|
|
|
dob<-as.Date(format(birth, format="18%y%m%d"), format="%Y%m%d")
|
|
|
|
}
|
|
|
|
else {print("Input contains data in wrong format") # test if position 5,6 or 8 contains letters as is the case for temporary cpr-numbers
|
|
|
|
}
|
|
|
|
dobs<-append(dobs,dob)
|
2018-10-04 17:04:15 +02:00
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
}
|
2018-10-04 17:04:15 +02:00
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
return(dobs)
|
2022-08-24 14:37:30 +02:00
|
|
|
|
2018-10-02 21:07:43 +02:00
|
|
|
}
|