A naxp_encoded vector holds encoded values together with the naxp that
encoded them, so it can always be decoded. naxp_encode() returns one, and
naxp_encoded() makes one from bare values, such as values read from a
database or a file.
Arguments
- values
An integer or double vector of whole numbers from 0 to the naxp's maximum encoded value.
NAis an error.- naxp
A naxp from
naxp_parse(), a pattern string or a naxp_encoded vector, whose naxp is used.
Details
The values are integers where the naxp's maximum encoded value is at most
2^31 - 1, and doubles where it is at most 2^53. Above 2^53 is an error. 0
stands for text that is not valid. Encoded values are never NA.
A naxp_encoded vector prints as the text it decodes to, with 0 shown as
<invalid (0)>. as.character() gives the text, with NA for 0, and
as.integer() or as.numeric() give the bare values.
Subsetting, rep(), rev(), unique() and sort() return naxp_encoded
vectors with the same naxp, and split() returns a list of them. A
naxp_encoded vector can be a data frame column. order() and match()
return integer indices, and table() counts each distinct value, labelled
with its text, excluding 0. Assigning into a naxp_encoded vector accepts
another with the same naxp, or text, which is encoded. c() combines
naxp_encoded vectors with the same naxp.
Encoded values are compared only for equality. == and != compare a
naxp_encoded vector with one with the same naxp, with text, which is
encoded first, or with numbers as bare values, so x == 0 finds the
invalid text. Arithmetic, <, > and summaries such as max() raise
errors. sort() and order() sort by encoded value.
Examples
nl_postcode <- naxp_parse(r"(\9\9\9\9 \s!! \A\A)")
codes <- naxp_encoded(c(684114, 0, 6760000), nl_postcode)
codes
#> <naxp_encoded[3]> \9\9\9\9 \s!! \A\A
#> [1] 1012 AB <invalid (0)> 9999 ZZ
codes == "1012 AB"
#> [1] TRUE FALSE FALSE
codes == 0
#> [1] FALSE TRUE FALSE
data.frame(postcode = codes)
#> postcode
#> 1 1012 AB
#> 2 <invalid (0)>
#> 3 9999 ZZ