sweidnumbr : Structural handling of swedish identity numbers

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)


Install the stable release version in R:


Test the installation by loading the library:


We also recommend setting the UTF-8 encoding:


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)

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


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


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


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, date = "2000-01-01")

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")
format_pin(example_pin, "%P")

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


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)

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


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


We can also check the type of organization.


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


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



Session info

This vignette was created with


Try the sweidnumbr package in your browser

Any scripts or data that you put into this service are public.

sweidnumbr documentation built on March 31, 2020, 5:24 p.m.