daDoctoR/R/cpr_check_function.R

27 lines
675 B
R
Raw Normal View History

2018-10-02 21:07:43 +02:00
#' CPR check
#'
#' Checking validity of cpr number
#' @param x cpr as "ddmmyy-xxxx".
#' @keywords cpr
#' @export
#' @examples
2018-10-04 17:04:15 +02:00
#' cpr_check("231045-0637")
2018-10-02 21:07:43 +02:00
cpr_check<-function(x){
#Check validity of CPR number, format ddmmyy-xxxx
2018-10-04 17:04:15 +02:00
2018-10-02 21:07:43 +02:00
p1<-as.integer(substr(x,1,1))
p2<-as.integer(substr(x,2,2))
p3<-as.integer(substr(x,3,3))
p4<-as.integer(substr(x,4,4))
p5<-as.integer(substr(x,5,5))
p6<-as.integer(substr(x,6,6))
p7<-as.integer(substr(x,8,8))
p8<-as.integer(substr(x,9,9))
p9<-as.integer(substr(x,10,10))
p10<-as.integer(substr(x,11,11))
2018-10-04 17:04:15 +02:00
2018-10-02 21:07:43 +02:00
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)
2018-10-04 17:04:15 +02:00
}