Type: | Package |
Title: | Handling of Swedish Identity Numbers |
Version: | 1.5.0 |
Date: | 2024-01-13 |
Author: | Mans Magnusson and Erik Bulow |
Maintainer: | Mans Magnusson <mons.magnusson@gmail.com> |
Description: | Structural handling of identity numbers used in the Swedish administration such as personal identity numbers ('personnummer') and organizational identity numbers ('organisationsnummer'). |
VignetteBuilder: | rmarkdown, knitr |
URL: | https://github.com/rOpenGov/sweidnumbr, http://ropengov.github.io/sweidnumbr/ |
BugReports: | https://github.com/rOpenGov/sweidnumbr/issues |
License: | BSD_2_clause + file LICENSE |
Depends: | R (≥ 3.2) |
Encoding: | UTF-8 |
Imports: | lubridate (≥ 1.5), stringr, checkmate |
Suggests: | testthat (≥ 1.0.0), rmarkdown, knitr |
LazyData: | true |
RoxygenNote: | 7.2.3 |
X-schema.org-isPartOf: | http://ropengov.org/ |
X-schema.org-keywords: | ropengov |
NeedsCompilation: | no |
Packaged: | 2024-01-13 20:05:27 UTC; manma760 |
Repository: | CRAN |
Date/Publication: | 2024-01-13 20:20:02 UTC |
Parse organizational identity numbers
Description
Check and convert a vector of organizational identity numbers.
Usage
as.oin(oin)
Arguments
oin |
Vector with swedish organizational identity numbers in character format. See details. |
Details
The following format is accepted:
character:
GNNNNN-NNNC
Value
Character vector (of class oin
and AsIs
) with swedish organizational identity numbers.
References
Lag (1974:174) om identitetsbeteckning for juridiska personer m.fl.
Examples
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "8020024280", "AA2002-4280")
as.oin(ex_oin)
Parse personal identity numbers to ABS format
Description
as.pin
Converts personal identity numbers of different formats to standard (ABS)
pin format YYYYMMDDNNNC
where YYYYMMDD
is the date of birth, NNN
is the birth number and C
is the
control number.
is.pin
checks wether an R object is of class "pin".
Usage
as.pin(pin)
is.pin(pin)
Arguments
pin |
Vector with swedish personal identity numbers in character or numeric format. See details. |
Details
as.pin
converts different formats of swedish personal identity numbers to
the standard ABS format. The formats that can be converted are:
numeric:
YYYYMMDDNNNC
numeric:
YYMMDDNNNC
(assuming < 100 years of age)character:
"YYYYMMDDNNNC"
character:
"YYMMDD-NNNC"
,"YYMMDD+NNNC"
character:
"YYYYMMDD-NNNC"
character:
"YYMMDDNNNC"
(assuming < 100 years of age)
(where "C" can be substituted by characters "A", "T" or "X" if "YYYY" < 1967).
Value
as.pin
returns a vector of class "pin" (with additional classes "AsIs" and character)
with swedish personal identity numbers with standard ABS format "YYYYMMDDNNNC"
.
is.pin
returns TRUE
if pin
is of class "pin", otherwise FALSE
.
References
-
Skatteverket, Population registration in Sweden, SKV 717B (2007)
-
Personnummer: information fran Centrala folkbokförings- och uppbördsnämnden. (1967). Stockholm
-
Den svenska folkbokföringens historia under tre sekel. (1982). Solna: Riksskatteverket URL
Examples
# Examples taken from SKV 704 (see references)
ex_pin1 <- c("196408233234", "640823-3234", "19640823-3234")
as.pin(pin = ex_pin1)
ex_pin2 <- c("6408233234")
as.pin(ex_pin2)
ex_pin3 <- c(6408233234, 196408233234)
as.pin(ex_pin3)
ex_pin4 <-rep(c("20121209-0122", "201212090122", "121209-0122", "1212090122"),250)
as.pin(ex_pin4)
ex_pin5 <-c("205012090122", "186512090122", "121209-0122", "121209-012A")
as.pin(pin = ex_pin5)
pin <-c("201212090122", "201212090122", "121209-0122", "1212090122")
ex_pin <- rpin(3)
is.pin(ex_pin)
ex_pin_char <- as.character(ex_pin)
is.pin(ex_pin_char)
Create S3 methods
Description
Helper function to create S3 method that preserves (most)
Usage
create_s3_method(generic = NULL, object = NULL)
Arguments
generic , object |
arguments passed to |
Value
Function with arguments "x", "i", and "...". The "i" argument can be thought of as "index" if the method defined is for example the "["-function.
Fake personal identity numbers and names
Description
Data set with fake personal identity numbers and names to use as example.
Format
A data frame with 62 rows and 2 variables:
- pin
Personal identification number, as character
- name
Fictional Swedish names
Formatting pin
Description
Format pin
for pretty printing
Usage
format_pin(x, format. = "%Y%m%d%N", ...)
Arguments
x |
vector of class "pin" (see |
format. |
character string specifying the output format.
|
... |
arguments passed to |
Value
character vector of same length as x
Examples
x <- as.pin(fake_pins$pin[1:10])
# Separate elements with hyphens:
format_pin(x, "%Y-%m-%d-%N")
# Separate even further
format_pin(x, "%C-%y-%m-%d-%N")
# The special P-format for maximal readability
format_pin(x, "%P")
# A custom representation
format_pin(x, "Borned %d of %B in %Y (a %A in week %U) with suffix no: %N")
# Extract only the year
format_pin(x, "%Y")
Test if a character vector contains correct oin
Description
Test which elements in a text vector that contains organization identity number.
Usage
is.oin(oin)
Arguments
oin |
Character vector to be tested if it is an |
Value
Logical vector indicating if the elements can be an organization identity number.
Examples
ex_oin <- roin(3)
is.oin(ex_oin)
ex_oin_char <- as.character(ex_oin)
is.oin(ex_oin_char)
The Luhn algorithm
Description
Calculates the control number for a Swedish personal/organisational identity number using the Luhn algorithm.
Usage
luhn_algo(id, multiplier)
Arguments
id |
Element with swedish personal identity number. |
multiplier |
What should each element in id be multiplied with |
Value
The control number (last digit in the personal identification number) calculated from id
(as integer).
References
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007)
Examples
luhn_algo("121212121212", c(0,0,2,1,2,1,2,1,2,1,2,0))
luhn_algo( "121212121", c( 2,1,2,1,2,1,2,1,2))
## If no multiplier, the default is
## to find one that match the format of id
luhn_algo("121212121212")
luhn_algo("12121212121")
luhn_algo("1212121212")
luhn_algo("121212121")
## Also for multiple pin
## (as long they are all of the same format)
luhn_algo(c("12121212121", "19850504333"))
## Not run:
try(luhn_algo(c("12121212121", "850504333"))) ## Different formats should fail!
## End(Not run)
Check the control numbers for oin
Description
Calculates the control number using the Luhn algorithm and compare it with the control number in the organization identity number (oin).
Usage
oin_ctrl(oin, force_logical = FALSE)
Arguments
oin |
A vector of class |
force_logical |
If TRUE, force all NA in oin to be FALSE. Default is FALSE. |
Value
Logical vector indicating if a oin is correct (TRUE
) or not (FALSE
)
References
Organisationsnummer Skatteverket
Examples
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "232100-0157", "802002-4281")
oin_ctrl(ex_oin)
Calculate organization group from oin
Description
Calculates the organization group from the organization number.
Usage
oin_group(oin)
Arguments
oin |
A vector of class |
Value
Factor with organization categories.
References
Organisationsnummer Skatteverket
Examples
ex_oin <- c("556000-4615", "232100-0156", "802002-4280")
oin_group(ex_oin)
oin_group_element
Description
Calculates the organization group from the organization number for one oin.
Usage
oin_group_element(one_oin)
Arguments
one_oin |
Character elemen with oin. |
Value
Character categegory of organisational group.
References
http://www.skatteverket.se/download/18.70ac421612e2a997f85800040284/1302507382017/70909.pdf
Calculate age of pin
for a given date
Description
Calculate the age in full years for a given date.
Usage
pin_age(pin, date = Sys.Date(), timespan = "years", verbose = TRUE)
Arguments
pin |
A vector of class |
date |
Date at which age is calculated. If a vector is provided it must be
of the same length as the |
timespan |
Timespan to use to calculate age. The actual timespans are:
|
verbose |
Should messages be printed? Default is |
Value
Age as an integer vector.
References
Skatteverket, Personnummer. SKV 704. (2007)
Examples
# Example with someone born today
today_pin <-
paste(paste(unlist(strsplit(as.character(Sys.Date()),split = "-")), collapse = ""),
"0000",sep="")
pin_age(today_pin)
# Examples taken from SKV 704 (see references)
ex_pin <- c("196408233234", "186408833224")
pin_age(ex_pin, date = "2012-01-01")
Calculate the birthplace of pin
Description
Calculate the birthplace for a given personal identity number born before 1990. See details.
Usage
pin_birthplace(pin)
Arguments
pin |
A vector of class |
Details
It is possible to calculate where people where born (and/or if a person has immigrated) through their personal identity number. This is possible for people that was born before 1990 and after 1945.
For people born before 1946 the birthplace identifier contains information on where one where registered the 1st of november 1946.
Personal identity numbers for people born after 1989 do not contain any information on birthplace.
During the period 1946 - 1989 the pin also contains information on whether one has immigrated to Sweden during the period.
Value
Birthplace as factor.
References
SOU 2008:60 : Personnummer och samordningsnummer
Examples
# Example with someone born today and from SKV 704 (see references)
today_pin <- paste0(format(Sys.Date(),"%Y%m%d"), "0000")
ex_pin <- c("196408233234", today_pin)
pin_birthplace(ex_pin)
pin_birthplace_internal
Description
Internal computation of birthplace (one for each pin)
Usage
pin_birthplace_internal(pin, birth_vector, birth_other_text)
Arguments
pin |
Character element with pin at standard format 'YYYYMMDDNNNC'. See as.pin. |
birth_vector |
Vector mapping birth number to birthplace. See pin_birthplace. |
birth_other_text |
Text to return if born >= 1990. See pin_birthplace. |
Value
Character element containing birthplace
pin_century
Description
Assuming that the person is less than 100 years old, calculate the century of birth.
Usage
pin_century(pin_short)
Arguments
pin_short |
Vector with swedish personal identity numbers on standard format. |
Value
Century vector in numeric format
pin_convert
Description
Converts one pin to standard format
Usage
pin_convert(pin, format)
Arguments
pin |
A character element of length one. |
format |
Which format should be converted. See as.pin. |
Value
Character element on standard format.
Check if pin
is a coordination number
Description
Calculate if the personal identity number is a coordination number.
Usage
pin_coordn(pin)
Arguments
pin |
A vector of class |
Value
Logical vector indicating if the pin is a coordination number (TRUE
) or pin (FALSE
).
References
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
Examples
# Examples taken from SKV 704 (see references)
ex_pin <- c("196408233234", "196408833224")
pin_coordn(ex_pin)
pin_coordn_correct
Description
Remove the change of day in coordination numbers (to enable age calculation).
Usage
pin_coordn_correct(pin)
Arguments
pin |
Vector of pins at format atandard fromat 'YYYYMMDDNNNC'. See as.pin. |
Value
Character vector with pin, corrected for coordination numbers.
Check control number from pin
Description
Calculates the control number using the Luhn algorithm and compare it with the control number in the personal identity number.
Usage
pin_ctrl(pin, force_logical = FALSE)
Arguments
pin |
A vector of class |
force_logical |
If TRUE, force all NA in pin to be FALSE. Default is FALSE. |
Value
Logical vector indicating if a pin is correct (TRUE
) or not (FALSE
)
References
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
Examples
# Examples taken from SKV 704 (see references)
ex_pin <- c("196408233234", "196408233235")
pin_ctrl(ex_pin)
Calculate the date of birth from a pin
Description
Calculates the date of birth in date format.
Usage
pin_date(pin)
Arguments
pin |
A vector of class |
Value
Date of birth as a vector in date format.
Examples
# Examples taken from SKV 704 (see references)
ex_pin <- c("196408233234", "186408833224")
pin_date(ex_pin)
Calculate sex from pin
Description
Calculates the sex from the personal identification number.
Usage
pin_sex(pin)
Arguments
pin |
A vector of class |
Value
Factor with label 'Male' and 'Female'.
References
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
Examples
# Examples taken from SKV 704 (see references)
ex_pin <- c("196408233234", "186408233224")
pin_sex(ex_pin)
Deprecated functions in package sweidnumbr.
Description
The functions listed below are deprecated and will be defunct in
the near future. When possible, alternative functions with similar
functionality are also mentioned. Help pages for deprecated functions are
available at help("-deprecated")
.
Usage
pin_to_date(pin)
pin_to_date
For pin_to_date
, use pin_date
.
Calculate the date of birth from a pin
Description
Calculates the date of birth in date format.
Usage
pin_to_date(pin)
Arguments
pin |
Swedish ID number |
Value
Date of birth as a vector in date format.
See Also
Generate a vector of random oin
Description
A function that generates random oin
s (see as.pin
).
The generated oin
is uniformely distributed over all possible oin
s.
Usage
roin(n)
Arguments
n |
number of observations. If |
Value
a vector of generated oin
s.
Examples
x <- roin(3)
oin_ctrl(x)
oin_group(x)
Generate a vector of random pin
Description
A function that generates random pin
s (see as.pin
).
The generated pin
is uniformely distributed over the time period.
Usage
rpin(
n,
start_date = "1900-01-01",
end_date = Sys.Date(),
p.male = 0.5,
p.coordn = 0.1
)
Arguments
n |
number of observations. If |
start_date |
Smallest possible |
end_date |
Largest possible |
p.male |
Proportion of males. Default is 0.5. |
p.coordn |
Proportion of coordination numbers. Default is 0.1. |
Value
a vector of generated pin
s.
Examples
x <- rpin(3)
pin_ctrl(x)
pin_sex(x)
pin_age(x)
sweidnumbr
Description
Handling of swedish identity numbers.
For a quick tutorial see vignette("sweidnumbr")
.
For more information see https://github.com/rOpenGov/sweidnumbr.