code: Code vector

View source: R/code.R

codeR Documentation

Code vector

Description

A code vector is a vector with an associated code list. The values in the vector should come from this code list. The values also have an associated label and optionally additional properties such as a description. See codelist for more information on what should and could be in a code list.

Usage

code(x, codelist, ...)

Arguments

x

vector to convert to code vector

codelist

code list to associate with the values in x. This should be convertable to codelist using as.codelist.

...

Ignored; used to pass extra arguments to other methods

Details

When codelist is omitted when case x is a factor, a code list is generated from the factor values.

Value

Returns an object of type 'code'. Except when x is a factor, x keeps classes and attributes assiated with x. This object is a copy of x with a codelist attribute added.

When x is a factor x it converted to an integer vector. The labels are the levels of the factor.

Examples


x <- code(c(1,4,2), codelist(codes = 1:4, labels = letters[1:4]))
print(x)
labels(x)

x <- code(factor(letters[1:3]))
print(x)
attr(x, "codelist")


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