mirror of
https://github.com/agdamsbo/daDoctoR.git
synced 2024-11-23 04:10:22 +01:00
updated dob_extract_cpr to also support cpr format ddmmyyxxxx 2022-08-25 09:42:37
This commit is contained in:
parent
9b81c4b34a
commit
d3cce910b8
@ -1,6 +1,6 @@
|
||||
Package: daDoctoR
|
||||
Title: Functions For Health Research
|
||||
Version: 0.22.4
|
||||
Version: 0.22.5
|
||||
Year: 2021
|
||||
Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
|
||||
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
|
||||
|
@ -1,14 +1,23 @@
|
||||
#' CPR check
|
||||
#'
|
||||
#' Checking validity of cpr number
|
||||
#' @param x cpr as "ddmmyy-xxxx".
|
||||
#' @param cpr cpr-numbers as ddmmyy[-.]xxxx or ddmmyyxxxx. Also mixed formatting.
|
||||
#' @keywords cpr
|
||||
#' @export
|
||||
#' @examples
|
||||
#' cpr_check("231045-0637")
|
||||
|
||||
cpr_check<-function(x){
|
||||
#Check validity of CPR number, format ddmmyy-xxxx
|
||||
cpr_check<-function(cpr){
|
||||
# Check validity of CPR number, format ddmmyy-xxxx
|
||||
# Build upon data from this document: https://cpr.dk/media/167692/personnummeret%20i%20cpr.pdf
|
||||
# example vector: fsd<-c("2310450637", "010115-4000", "0101896000","010189-3000","300450-1030","010150-4021")
|
||||
|
||||
v <- c()
|
||||
|
||||
for (x in cpr){
|
||||
if (!substr(x,7,7)%in%c("-",".")){ # Added check to take p8 if ddmmyy[-.]xxxx,
|
||||
x<-paste(substr(x,1,6),substr(x,7,10),collapse="-")
|
||||
}
|
||||
|
||||
p1<-as.integer(substr(x,1,1))
|
||||
p2<-as.integer(substr(x,2,2))
|
||||
@ -21,6 +30,10 @@ cpr_check<-function(x){
|
||||
p9<-as.integer(substr(x,10,10))
|
||||
p10<-as.integer(substr(x,11,11))
|
||||
|
||||
result<-ifelse((p1*4+p2*3+p3*2+p4*7+p5*6+p6*5+p7*4+p8*3+p9*2+p10) %% 11 == 0,"valid","invalid")
|
||||
return(result)
|
||||
v<-c(v,
|
||||
ifelse((p1*4+p2*3+p3*2+p4*7+p5*6+p6*5+p7*4+p8*3+p9*2+p10) %% 11 == 0,"valid","invalid")
|
||||
)
|
||||
}
|
||||
|
||||
return(v)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#' Extracting date of birth from CPR
|
||||
#'
|
||||
#' For easy calculation.
|
||||
#' @param cpr cpr-numbers in format ddmmyy-xxxx.
|
||||
#' @param cpr cpr-numbers as ddmmyy[-.]xxxx or ddmmyyxxxx. Also mixed formatting.
|
||||
#' @keywords cpr
|
||||
#' @export
|
||||
#' @examples
|
||||
@ -11,22 +11,11 @@
|
||||
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")
|
||||
## example vector: fsd<-c("010190-2000", "010115-4000", "0101896000","010189-3000","300450-1030","010150-4021")
|
||||
## cpr <- "231045-0637"
|
||||
## cpr <- "2310450637"
|
||||
{
|
||||
|
||||
if (any(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
|
||||
}
|
||||
|
||||
|
||||
dobs<-c()
|
||||
|
||||
a00<-as.numeric(c(0:99))
|
||||
@ -40,7 +29,9 @@ dob_extract_cpr<-function(cpr)
|
||||
{
|
||||
p56<-as.numeric(substr(x,5,6))
|
||||
|
||||
if (cpr_std){p8<-as.numeric(substr(x,8,8))} else {p8<-as.numeric(substr(x,9,9))}
|
||||
if (substr(x,7,7)%in%c("-",".")){
|
||||
p8<-as.numeric(substr(x,8,8)) # Added check to take p8 if ddmmyy[-.]xxxx,
|
||||
} else {p8<-as.numeric(substr(x,7,7))} # or p7 if ddmmyyxxxx
|
||||
|
||||
birth<-as.Date(substr(x,1,6),format="%d%m%y")
|
||||
|
||||
|
@ -4,10 +4,10 @@
|
||||
\alias{cpr_check}
|
||||
\title{CPR check}
|
||||
\usage{
|
||||
cpr_check(x)
|
||||
cpr_check(cpr)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{cpr as "ddmmyy-xxxx".}
|
||||
\item{cpr}{cpr-numbers as ddmmyy[-.]xxxx or ddmmyyxxxx. Also mixed formatting.}
|
||||
}
|
||||
\description{
|
||||
Checking validity of cpr number
|
||||
|
@ -7,7 +7,7 @@
|
||||
dob_extract_cpr(cpr)
|
||||
}
|
||||
\arguments{
|
||||
\item{cpr}{cpr-numbers in format ddmmyy-xxxx.}
|
||||
\item{cpr}{cpr-numbers as ddmmyy[-.]xxxx or ddmmyyxxxx. Also mixed formatting.}
|
||||
}
|
||||
\description{
|
||||
For easy calculation.
|
||||
|
Loading…
Reference in New Issue
Block a user