sweidnumbr : Structural handling of swedish identity numbers

Mans Magnusson and Erik Bulow

2020-03-29

This R package provides tools to work with swedish identity numbers such as personal identity numbers (personnummer) and organizational identity numbers (organisationsnummer).

Table of contents

Installation (Installation)

Example: personal identity numbers (Personal identity numbers)

Example: organizational identity numbers (Organizational identity number)

Licensing and Citations (Licensing and Citations)

References (References)

Installation

Install the stable release version in R:

install.packages("sweidnumbr")

Test the installation by loading the library:

library(sweidnumbr)

We also recommend setting the UTF-8 encoding:

Sys.setlocale(locale="UTF-8") 

Example: personal identity numbers

As a first step we need to convert personal identity numbers (pin) to the same standard format used by the Swedish tax authority.

example_pin <- c("640823-3234", "6408233234", "19640823-3230")
example_pin <- as.pin(example_pin)
example_pin
## [1] "196408233234" "196408233234" "196408233230"
## Personal identity number(s)

The next step is to test if the vector is a pin object. To do this we use the is.pin() function.

is.pin(example_pin)
## [1] TRUE

This only check the format of the pin. To check the pin using the control number we use pin_ctrl().

pin_ctrl(example_pin)
## [1]  TRUE  TRUE FALSE

We can now use pin_birthplace() and pin_sex(). To get information on sex and birthplace.

pin_sex(example_pin)
## [1] Male Male Male
## Levels: Male
pin_birthplace(example_pin)
## [1] Gotlands län Gotlands län Gotlands län
## 28 Levels: Stockholm stad Stockholms län Uppsala län ... Born after 31 december 1989

As the last step we can calculate the age based on the pin. We choose the date where we want to calculate the age. If date is not specified the current date is used.

pin_age(example_pin)
## [1] 55 55 55
pin_age(example_pin, date = "2000-01-01")
## [1] 35 35 35

It is also possible to format the pin for presentation in different forms. (Note however that the output of format_pin is just a character and no longer a pin object):

format_pin(example_pin, "%Y-%m-%d-%N")
## [1] "1964-08-23-3234" "1964-08-23-3234" "1964-08-23-3230"
format_pin(example_pin, "%P")
## [1] "(19) 64-08-23 - 3234" "(19) 64-08-23 - 3234" "(19) 64-08-23 - 3230"

Sometimes we want some example pins. We can easily simulate pins using rpin():

rpin(3)
## [1] "192611110961" "199406204404" "201908190725"
## Personal identity number(s)

Example: organizational identity numbers

Handling of organizational identity numbers is done in a similar fashion. But organizational numbers are only allowed to have one format.

example_oin <- c("556000-4615", "232100-0156", "802002-4280")
example_oin <- as.oin(example_oin)
example_oin
## [1] "556000-4615" "232100-0156" "802002-4280"
## Organizational identity number(s)

We can test if the vector has a correct format in a similar way as for pin.

is.oin(example_oin)
## [1] TRUE

With a vector of oin we can check if the organizational number is correct.

oin_ctrl(example_oin)
## [1] TRUE TRUE TRUE

We can also check the type of organization.

oin_group(example_oin)
## [1] Aktiebolag                             
## [2] Stat, landsting, kommuner, församlingar
## [3] Ideella föreningar och stiftelser      
## 3 Levels: Aktiebolag ... Stat, landsting, kommuner, församlingar

Sometimes we want some example oins. We can easily simulate oins using roin():

roin(3)
## [1] "964228-2835" "945463-1814" "736937-8059"
## Organizational identity number(s)

Licensing and Citations

This work can be freely used, modified and distributed under the open license specified in the DESCRIPTION file.

Kindly cite the work as follows

citation("sweidnumbr")
## 
## Kindly cite the sweidnumbr R package as follows:
## 
##   Magnusson, Mans and Bulow, Erik (2020). sweidnumbr: R tools to handle
##   of swedish identity numbers. R package version 1.4.2 URL:
##   http://github.com/rOpenGov/sweidnumbr
## 
## A BibTeX entry for LaTeX users is
## 
##   @Misc{,
##     title = {sweidnumbr: R tools to handle of swedish identity numbers.},
##     author = {Mans Magnusson and Erik Bulow},
##     url = {http://github.com/rOpenGov/sweidnumbr},
##     year = {2020},
##     note = {R package version 1.4.2},
##   }

References

Session info

This vignette was created with

sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Catalina 10.15.3
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] C/sv_SE.UTF-8/sv_SE.UTF-8/C/sv_SE.UTF-8/sv_SE.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] sweidnumbr_1.4.2
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.3      lubridate_1.7.4 digest_0.6.25   backports_1.1.5
##  [5] magrittr_1.5    evaluate_0.14   rlang_0.4.5     stringi_1.4.5  
##  [9] checkmate_2.0.0 rmarkdown_1.18  tools_3.6.1     stringr_1.4.0  
## [13] xfun_0.11       yaml_2.2.0      compiler_3.6.1  htmltools_0.4.0
## [17] knitr_1.26