Decode (unpack) a bitfield

bf_decode(x, registry, flags = NULL, sep = NULL, verbose = TRUE)

Arguments

x

integerish(1)
table of the integer representation of the bitfield.

registry

registry(1)
the registry that should be used to decode the bitfield.

flags

character(.)
the name(s) of flags to extract from this bitfield; leave at NULL to extract the full bitfield.

sep

character(1)
a symbol with which, if given, the distinct fields shall be separated.

verbose

logical(1)
whether or not to return the registry legend.

Examples

# grow registry
reg <- bf_na(x = tbl_bityield, test = "commodity")
reg <- bf_match(x = tbl_bityield, test = "commodity", set = c("soybean", "maize"),
                registry = reg)
reg
#>   width 2
#>   flags 2  -|-
#> 
#>   pos   encoding type   col
#>   1     0.0.1/0  na     commodity
#>   2     0.0.1/0  match  commodity

# encode the flags into a bitfield
field <- bf_encode(registry = reg)
field
#> # A tibble: 10 × 1
#>    bf_int1
#>      <int>
#>  1       1
#>  2       1
#>  3       2
#>  4       1
#>  5       0
#>  6       1
#>  7       1
#>  8       1
#>  9       1
#> 10       1

# decode (somewhere downstream)
flags <- bf_decode(x = field, registry = reg, sep = "-")
#> # A tibble: 4 × 4
#> # Rowwise: 
#>   pos   name            flag  desc                                              
#>   <chr> <chr>           <chr> <chr>                                             
#> 1 1     na_commodity    0     {FALSE} the value in column 'commodity' is not NA.
#> 2 1     na_commodity    1     {TRUE}  the value in column 'commodity' is NA.    
#> 3 2     match_commodity 0     {FALSE} the value in column 'commodity' is not in…
#> 4 2     match_commodity 1     {TRUE}  the value in column 'commodity' is includ…
flags
#> # A tibble: 10 × 1
#>    bf_bin
#>    <chr> 
#>  1 0-1   
#>  2 0-1   
#>  3 1-0   
#>  4 0-1   
#>  5 0-0   
#>  6 0-1   
#>  7 0-1   
#>  8 0-1   
#>  9 0-1   
#> 10 0-1   

# more reader friendly
cbind(tbl_bityield, bf_decode(x = field, registry = reg, verbose = FALSE))
#>        x    y commodity     yield  year na_commodity match_commodity
#> 1   25.3 59.5   soybean 11.192915  2021            0               1
#> 2   27.9 58.1     maize 11.986793  <NA>            0               1
#> 3   27.8 57.8      <NA> 13.229386 2021r            1               0
#> 4   27.0 59.2     maize  4.431376  2021            0               1
#> 5  259.0  Inf     honey 12.997422  2021            0               0
#> 6   27.3 59.1     maize  8.548882  2021            0               1
#> 7   26.1 58.4   soybean 11.276921  2021            0               1
#> 8   26.5 59.0     maize 10.640715  2021            0               1
#> 9    0.0  0.0   soybean  9.010452  2021            0               1
#> 10  25.7  NaN     maize 13.169897  2021            0               1