---
title: "sweidnumbr : Structural handling of swedish identity numbers"
author: "Mans Magnusson and Erik Bulow"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{sweidnumbr}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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) (Installation)  

[Example: personal identity numbers](#examplepin) (Personal identity numbers)

[Example: organizational identity numbers](#exampleoin) (Organizational identity number)

[Licensing and Citations](#licens) (Licensing and Citations)

[References](#references) (References)  

## <a name="installation"></a>Installation

Install the stable release version in R:

```{r install, eval=FALSE}
install.packages("sweidnumbr")
```

Test the installation by loading the library:

```{r test, message=FALSE, warning=FALSE, eval=TRUE}
library(sweidnumbr)
```

We also recommend setting the UTF-8 encoding:

```{r locale, eval=FALSE}
Sys.setlocale(locale="UTF-8") 
```

## <a name="examplepin"></a>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.

```{r example1, message=FALSE, eval=TRUE}
example_pin <- c("640823-3234", "6408233234", "19640823-3230")
example_pin <- as.pin(example_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.

```{r example2, message=FALSE, eval=TRUE}
is.pin(example_pin)
```

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

```{r example3, message=FALSE, eval=TRUE}
pin_ctrl(example_pin)
```

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

```{r example4, message=FALSE, eval=TRUE}
pin_sex(example_pin)
pin_birthplace(example_pin)
```

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.

```{r example5, message=FALSE, eval=TRUE}
pin_age(example_pin)
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):

```{r example6, message=FALSE, eval=TRUE}
format_pin(example_pin, "%Y-%m-%d-%N")
format_pin(example_pin, "%P")
```

Sometimes we want some example `pin`s. We can easily simulate `pin`s using `rpin()`:

```{r example_rpin, message=FALSE, eval=TRUE}
rpin(3)
```


## <a name="exampleoin"></a>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.

```{r oin1, message=FALSE, eval=TRUE}
example_oin <- c("556000-4615", "232100-0156", "802002-4280")
example_oin <- as.oin(example_oin)
example_oin
```

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

```{r oin2, message=FALSE, eval=TRUE}
is.oin(example_oin)
```

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

```{r oin3, message=FALSE, eval=TRUE}
oin_ctrl(example_oin)
```

We can also check the type of organization. 

```{r oin4, message=FALSE, eval=TRUE}
oin_group(example_oin)
```

Sometimes we want some example `oin`s. We can easily simulate `oin`s using `roin()`:

```{r example_roin, message=FALSE, eval=TRUE}
roin(3)
```


## <a name="licens"></a>Licensing and Citations

This work can be freely used, modified and distributed under the open license specified in the [DESCRIPTION file](https://github.com/rOpenGov/sweidnumbr/blob/master/DESCRIPTION).

Kindly cite the work as follows

```{r citation, message=FALSE, eval=TRUE}
citation("sweidnumbr")
```


## <a name="references"></a>References 

- [Population registration in Sweden](https://github.com/rOpenGov/sweidnumbr/blob/master/docs/skv717B-4.pdf). (2007)
- [SKV 704 : Personnummer](https://github.com/rOpenGov/sweidnumbr/blob/master/docs/skv704-8.pdf). (2007)
- [SOU 2008:60 : Personnummer och samordningsnummer](https://www.riksdagen.se/sv/dokument-och-lagar/dokument/statens-offentliga-utredningar/personnummer-och-samordningsnummer-del-1_gwb360/). (2008)
- Personnummer: information fran Centrala folkbokförings- och uppbördsnämnden. (1967). Stockholm
- Den svenska folkbokföringens historia under tre sekel. (1982). Solna: Riksskatteverket [URL](http://www.skatteverket.se/privat/folkbokforing/omfolkbokforing/folkbokforingigaridag/densvenskafolkbokforingenshistoriaundertresekler.4.18e1b10334ebe8bc80004141.html)
- [Lag (1974:174) om identitetsbeteckning for juridiska personer m.fl.](https://www.riksdagen.se/sv/dokument-och-lagar/dokument/svensk-forfattningssamling/lag-1974174-om-identitetsbeteckning-for_sfs-1974-174/)


## Session info

This vignette was created with

```{r sessioninfo, message=FALSE, warning=FALSE}
sessionInfo()
```
