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)
integerish(1)
table of the integer representation of
the bitfield.
registry(1)
the registry that should be used
to decode the bitfield.
character(.)
the name(s) of flags to extract
from this bitfield; leave at NULL
to extract the full bitfield.
character(1)
a symbol with which, if given, the
distinct fields shall be separated.
logical(1)
whether or not to return the
registry legend.
data.frame with the binary values of flags in the registry in columns.
# build registry
reg <- bf_registry(name = "testBF", description = "test bitfield")
reg <- bf_map(protocol = "na", data = bf_tbl, registry = reg, x = commodity)
reg <- bf_map(protocol = "matches", data = bf_tbl, registry = reg,
x = commodity, set = c("soybean", "maize"))
reg
#> width 2
#> flags 2 -|-
#>
#> pos encoding type col
#> 1 0.0.1/0 na commodity
#> 2 0.0.1/0 matches commodity
# encode the flags into a bitfield
field <- bf_encode(registry = reg)
field
#> # A tibble: 9 × 1
#> bf_int1
#> <int>
#> 1 1
#> 2 1
#> 3 1
#> 4 2
#> 5 0
#> 6 1
#> 7 1
#> 8 1
#> 9 1
# decode (somewhere downstream)
flags <- bf_decode(x = field, registry = reg, sep = "-")
#> # A tibble: 2 × 3
#> pos name desc
#> <chr> <chr> <chr>
#> 1 1:1 na_commodity 'commodity' contains NA-values.
#> 2 2:2 matches_commodity The 'commodity' values match the set 'soybean, maize'.
flags
#> # A tibble: 9 × 1
#> bf_bin
#> <chr>
#> 1 0-1
#> 2 0-1
#> 3 0-1
#> 4 1-0
#> 5 0-0
#> 6 0-1
#> 7 0-1
#> 8 0-1
#> 9 0-1
# more reader friendly
cbind(bf_tbl, bf_decode(x = field, registry = reg, verbose = FALSE))
#> x y commodity yield year na_commodity matches_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 soybean 13.229386 2021r 0 1
#> 4 27.0 59.2 <NA> 4.431376 2021 1 0
#> 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 NaN maize 10.640715 2021 0 1
#> 9 0.0 0.0 soybean 9.010452 2021 0 1