encode {encode}R Documentation

Encode Factor-like Levels and Labels as a Simple String

Description

For compact storage, encode combines a set of levels and labels (codes and decodes) into a simple string. The default method converts its argument to character. The list method operates element-wise, expecting an equal number of label elements, each of which have the same length as the corresponding element of x.

Usage

encode(x, ...)

Arguments

x

object

...

passed arguments

Details

An empty 'encoding' consists of four identical characters, e.g. ////.

A non-empty encoding must be at least 5 characters long, beginning and ending with two instances of sep e.g. //1//. Levels are likewise separated from each other by double separators, e.g. //1//2//.

If a label (decode) is available for a level, it follows the corresponding level: the two are separated by a single instance of sep, e.g. //1/a//2/b//.

Encodings may be combined as elements of a character vector, i.e. and encoded vector. Choice of separator may vary among elements, but must be consistent within elements.

Labels (decodes) may be zero-length, but not levels (codes), e.g. //1/// is valid but ///a// is not. A zero-length decode is extracted as an empty string.

Duplicate levels (codes) result in a warning for encode(), and are otherwise silently ignored. Duplicate labels (decodes) result in case-collapsing.

See Also

encode.character encode.default encode.list codes decodes decode encoded

Other encode: encode.character, encode.default, encode.list

Examples


a <- encode(
  x = list(
    c('M','F'),
    c(1:4)
  ),
  labels = list(
    c('male','female'),
    c('caucasian','asian','african',NA)
  )
)
b <- encode(c(1:2),c('pediatric','adult'))
a
b
c <- c('a',NA,'##b##')
encoded(a)
encoded(b)
encoded(c)
encoded(' //4// ')
codes(a)
codes(b)
codes(b,simplify=FALSE)
codes(c)
codes('..1..')
decodes(a)
decodes(b)
decodes(c)
decode(1:4,'//1/a//2/b//3/c//')
decode(1:4,'//1/a//1/b//3/c//') # duplicate code: ignored
decode(1:4,'//1/a//2/a//3/c//') # duplicate decode: collapsed
# encode(c(1,1,2,3),c('a','b','c','d')) Warning: duplicate codes



[Package encode version 0.3.6 Index]