Title: | Handling of Swedish Identity Numbers |
---|---|
Description: | Structural handling of identity numbers used in the Swedish administration such as personal identity numbers ('personnummer') and organizational identity numbers ('organisationsnummer'). |
Authors: | Mans Magnusson and Erik Bulow |
Maintainer: | Mans Magnusson <[email protected]> |
License: | BSD_2_clause + file LICENSE |
Version: | 1.5.0 |
Built: | 2025-01-30 11:23:36 UTC |
Source: | https://github.com/rOpenGov/sweidnumbr |
Check and convert a vector of organizational identity numbers.
as.oin(oin)
as.oin(oin)
oin |
Vector with swedish organizational identity numbers in character format. See details. |
The following format is accepted:
character: GNNNNN-NNNC
Character vector (of class oin
and AsIs
) with swedish organizational identity numbers.
Lag (1974:174) om identitetsbeteckning for juridiska personer m.fl.
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "8020024280", "AA2002-4280") as.oin(ex_oin)
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "8020024280", "AA2002-4280") as.oin(ex_oin)
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".
as.pin(pin) is.pin(pin)
as.pin(pin) is.pin(pin)
pin |
Vector with swedish personal identity numbers in character or numeric format. See 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).
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
.
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 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)
# 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)
Data set with fake personal identity numbers and names to use as example.
A data frame with 62 rows and 2 variables:
Personal identification number, as character
Fictional Swedish names
Format pin
for pretty printing
format_pin(x, format. = "%Y%m%d%N", ...)
format_pin(x, format. = "%Y%m%d%N", ...)
x |
vector of class "pin" (see |
format. |
character string specifying the output format.
|
... |
arguments passed to |
character vector of same length as x
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")
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")
oin
Test which elements in a text vector that contains organization identity number.
is.oin(oin)
is.oin(oin)
oin |
Character vector to be tested if it is an |
Logical vector indicating if the elements can be an organization identity number.
ex_oin <- roin(3) is.oin(ex_oin) ex_oin_char <- as.character(ex_oin) is.oin(ex_oin_char)
ex_oin <- roin(3) is.oin(ex_oin) ex_oin_char <- as.character(ex_oin) is.oin(ex_oin_char)
Calculates the control number for a Swedish personal/organisational identity number using the Luhn algorithm.
luhn_algo(id, multiplier)
luhn_algo(id, multiplier)
id |
Element with swedish personal identity number. |
multiplier |
What should each element in id be multiplied with |
The control number (last digit in the personal identification number) calculated from id
(as integer).
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007)
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)
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)
oin
Calculates the control number using the Luhn algorithm and compare it with the control number in the organization identity number (oin).
oin_ctrl(oin, force_logical = FALSE)
oin_ctrl(oin, force_logical = FALSE)
oin |
A vector of class |
force_logical |
If TRUE, force all NA in oin to be FALSE. Default is FALSE. |
Logical vector indicating if a oin is correct (TRUE
) or not (FALSE
)
Organisationsnummer Skatteverket
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "232100-0157", "802002-4281") oin_ctrl(ex_oin)
ex_oin <- c("556000-4615", "232100-0156", "802002-4280", "232100-0157", "802002-4281") oin_ctrl(ex_oin)
oin
Calculates the organization group from the organization number.
oin_group(oin)
oin_group(oin)
oin |
A vector of class |
Factor with organization categories.
Organisationsnummer Skatteverket
ex_oin <- c("556000-4615", "232100-0156", "802002-4280") oin_group(ex_oin)
ex_oin <- c("556000-4615", "232100-0156", "802002-4280") oin_group(ex_oin)
pin
for a given dateCalculate the age in full years for a given date.
pin_age(pin, date = Sys.Date(), timespan = "years", verbose = TRUE)
pin_age(pin, date = Sys.Date(), timespan = "years", verbose = TRUE)
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 |
Age as an integer vector.
Skatteverket, Personnummer. SKV 704. (2007)
# 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")
# 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")
pin
Calculate the birthplace for a given personal identity number born before 1990. See details.
pin_birthplace(pin)
pin_birthplace(pin)
pin |
A vector of class |
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.
Birthplace as factor.
SOU 2008:60 : Personnummer och samordningsnummer
# 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)
# 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
is a coordination numberCalculate if the personal identity number is a coordination number.
pin_coordn(pin)
pin_coordn(pin)
pin |
A vector of class |
Logical vector indicating if the pin is a coordination number (TRUE
) or pin (FALSE
).
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "196408833224") pin_coordn(ex_pin)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "196408833224") pin_coordn(ex_pin)
pin
Calculates the control number using the Luhn algorithm and compare it with the control number in the personal identity number.
pin_ctrl(pin, force_logical = FALSE)
pin_ctrl(pin, force_logical = FALSE)
pin |
A vector of class |
force_logical |
If TRUE, force all NA in pin to be FALSE. Default is FALSE. |
Logical vector indicating if a pin is correct (TRUE
) or not (FALSE
)
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "196408233235") pin_ctrl(ex_pin)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "196408233235") pin_ctrl(ex_pin)
pin
Calculates the date of birth in date format.
pin_date(pin)
pin_date(pin)
pin |
A vector of class |
Date of birth as a vector in date format.
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "186408833224") pin_date(ex_pin)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "186408833224") pin_date(ex_pin)
pin
Calculates the sex from the personal identification number.
pin_sex(pin)
pin_sex(pin)
pin |
A vector of class |
Factor with label 'Male' and 'Female'.
Skatteverket, Population registration in Sweden. SKV 717B. (2007)
Skatteverket, Personnummer. SKV 704. (2007) SOU 2008:60 : Personnummer och samordningsnummer, (2008)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "186408233224") pin_sex(ex_pin)
# Examples taken from SKV 704 (see references) ex_pin <- c("196408233234", "186408233224") pin_sex(ex_pin)
oin
A function that generates random oin
s (see as.pin
).
The generated oin
is uniformely distributed over all possible oin
s.
roin(n)
roin(n)
n |
number of observations. If |
a vector of generated oin
s.
x <- roin(3) oin_ctrl(x) oin_group(x)
x <- roin(3) oin_ctrl(x) oin_group(x)
pin
A function that generates random pin
s (see as.pin
).
The generated pin
is uniformely distributed over the time period.
rpin( n, start_date = "1900-01-01", end_date = Sys.Date(), p.male = 0.5, p.coordn = 0.1 )
rpin( n, start_date = "1900-01-01", end_date = Sys.Date(), p.male = 0.5, p.coordn = 0.1 )
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. |
a vector of generated pin
s.
x <- rpin(3) pin_ctrl(x) pin_sex(x) pin_age(x)
x <- rpin(3) pin_ctrl(x) pin_sex(x) pin_age(x)
Handling of swedish identity numbers.
For a quick tutorial see vignette("sweidnumbr")
.
For more information see https://github.com/rOpenGov/sweidnumbr.