as.codelist: Convert an object to a codelist object

View source: R/as.codelist.R

as.codelistR Documentation

Convert an object to a codelist object

Description

Convert an object to a codelist object

Usage

as.codelist(x, ...)

## S3 method for class 'codelist'
as.codelist(x, ...)

## S3 method for class 'data.frame'
as.codelist(
  x,
  code = names(x)[1],
  label = names(x)[2],
  description = "description",
  parent = "parent",
  locale = "locale",
  missing = "missing",
  format = c("regular", "wide"),
  locales = NULL,
  locale_sep = "[-_@. ]",
  ...
)

Arguments

x

data.frame with the code list

...

used to pass extra arguments on to other methods.

code

the name of the column in x containing the codes.

label

the name of the column in x containing the labels of the codes.

description

the name of the column in x containing the labels of the codes.

parent

the name of the column in x containing the parents of the codes in case of a hierarchical code list.

locale

the name of the column in x containing the locale of the corresponding row.

missing

the name of the column in x indicating whether or not a given code should be treated as missing values.

format

the format of data.frame. In case of 'wide', it is assummed that columns are repeated for each locale. For example there are columns 'label_locale1' and 'label_locale2'. In case of 'regular' there are multiple rows one for each locale.

locales

only used for format = "wide". The locales in the data set.

locale_sep

the separator separating the locale from the column name. This is interpreted as a regular expression (see the 'split' argument of strsplit). The part of the column name until the first separator is the column name; the remainder the locale name.

Details

When there is no column with the name given by label in x, a new column 'label' is derived containing codes converted to character.

Value

Returns a codelist object which is a data.frame with at minimum the columns 'code' and 'label' and optionally 'description', 'parent', 'locale' and 'missing'. When x contains additional columns these are kept.

See Also

codelist for a description of the codelist object.

Examples


# Examples below show the same codelist in both regular and wide format
dta <- data.frame(codes = c(1:3, 1:3), 
  labels = c(letters[1:3], LETTERS[1:3]), 
  locale = c("en", "en", "en", "nl" ,"nl" ,"nl"))
as.codelist(dta, format = "regular")

dta <- data.frame(codes = 1:3, labels_en = letters[1:3], 
  labels_nl = LETTERS[1:3])
as.codelist(dta, format = "wide")


codelist documentation built on April 12, 2025, 2:25 a.m.