This function takes an integer bitfield and the registry used to build it upstream to decode it into bit representation and thereby unpack the data stored in the 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

# build registry
reg <- bf_map(protocol = "na", data = bf_tbl, x = commodity)
reg <- bf_map(protocol = "matches", data = bf_tbl, x = 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  matches commodity-c-soybean-maize

# 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: 2 × 4
#> # Rowwise: 
#>   pos   name                              flag  desc                            
#>   <chr> <chr>                             <chr> <chr>                           
#> 1 1     na_commodity                      x     'commodity' contains NA-values. 
#> 2 2     matches_commodity-c-soybean-maize x     The 'commodity' values match th…
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(bf_tbl, bf_decode(x = field, registry = reg, verbose = FALSE))
#>        x    y commodity     yield  year na_commodity
#> 1   25.3 59.5   soybean 11.192915  2021            0
#> 2   27.9 58.1     maize 11.986793  <NA>            0
#> 3   27.8 57.8      <NA> 13.229386 2021r            1
#> 4   27.0 59.2     maize  4.431376  2021            0
#> 5  259.0  Inf     honey 12.997422  2021            0
#> 6   27.3 59.1     maize  8.548882  2021            0
#> 7   26.1 58.4   soybean 11.276921  2021            0
#> 8   26.5 59.0     maize 10.640715  2021            0
#> 9    0.0  0.0   soybean  9.010452  2021            0
#> 10  25.7  NaN     maize 13.169897  2021            0
#>    matches_commodity-c-soybean-maize
#> 1                                  1
#> 2                                  1
#> 3                                  0
#> 4                                  1
#> 5                                  0
#> 6                                  1
#> 7                                  1
#> 8                                  1
#> 9                                  1
#> 10                                 1