logic_map conducts an existence test of a vector or list of tuples (fvec) against a vector of unique values (bvec) optionally mapping a supplied vector of values (avec) to all TRUE results. The "vanilla" run mode produces at one-hot encoded version of fvec, but it can produce augmented versions of of such depending on the structure of fvec.

logic_map(
  fvec,
  avec = 1L,
  bvec = NULL,
  logical.out = FALSE,
  regex = FALSE,
  chatty = FALSE,
  ...
)

Arguments

fvec

(vector) Values to be tested: may be a simple vector or a list of tuples

avec

(vector) Optional vector of numeric values to project across the result (must be the same length as fvec or length-1)

bvec

(vector) A vector of unique values forming the basis of comparison If given as a named vector, the output will preserve the names when creating columns; otherwise, the values of bvec are used as the names.

logical.out

(logical, vector) When TRUE, the output consists of logical values; when a vector of length two (2) is supplied, the first value returns on FALSE, the second on TRUE; otherwise, the values of avec are used. If the vector form is used, only the first value at each position is used to supply the choices.

regex

(logical | FALSE) When TRUE, argument bvec is interpreted as patterns against which case-sensitive matches are sought are attempted. This forces the value of test to invoke like.

...

See stri_detect_regex: arguments str and pattern are internally passed and not needed.

Value

An array, the column names being the values of bvec or names of bvec if they exist.

Note

length(fvec) == length(avec)

fvec and bvec must be of compatible types for comparison.

Warning

When combining with a source object, fvec must NOT be sorted during the function call or the values will not map correctly in the output.

Examples

x <- LETTERS[1:10]
logic_map(x)
#>       A B C D E F G H I J
#>  [1,] 1 0 0 0 0 0 0 0 0 0
#>  [2,] 0 1 0 0 0 0 0 0 0 0
#>  [3,] 0 0 1 0 0 0 0 0 0 0
#>  [4,] 0 0 0 1 0 0 0 0 0 0
#>  [5,] 0 0 0 0 1 0 0 0 0 0
#>  [6,] 0 0 0 0 0 1 0 0 0 0
#>  [7,] 0 0 0 0 0 0 1 0 0 0
#>  [8,] 0 0 0 0 0 0 0 1 0 0
#>  [9,] 0 0 0 0 0 0 0 0 1 0
#> [10,] 0 0 0 0 0 0 0 0 0 1

k <- c("steelblue2", "tan3", "gray47", "gray27", "gray60")
x <- replicate(5, sample(k, 2) |> sort(), simplify = FALSE)
v <- sample(50, length(x), TRUE)
k
#> [1] "steelblue2" "tan3"       "gray47"     "gray27"     "gray60"    
v
#> [1] 34 49 37 48 36
str(x)
#> List of 5
#>  $ : chr [1:2] "gray60" "tan3"
#>  $ : chr [1:2] "gray47" "tan3"
#>  $ : chr [1:2] "gray47" "gray60"
#>  $ : chr [1:2] "gray47" "tan3"
#>  $ : chr [1:2] "gray27" "gray47"
# Vanilla:
# One-hot, default names:
logic_map(x)
#>      gray60 tan3 gray47 gray27
#> [1,]      1    1      0      0
#> [2,]      0    1      1      0
#> [3,]      1    0      1      0
#> [4,]      0    1      1      0
#> [5,]      0    0      1      1
# One-hot, custom names:
logic_map(x, bvec = k)
#>      steelblue2 tan3 gray47 gray27 gray60
#> [1,]          0    1      0      0      1
#> [2,]          0    1      1      0      0
#> [3,]          0    0      1      0      1
#> [4,]          0    1      1      0      0
#> [5,]          0    0      1      1      0
# One-hot, custom names, logical output:
logic_map(x, bvec = k, logical.out = TRUE)
#>      steelblue2  tan3 gray47 gray27 gray60
#> [1,]      FALSE  TRUE  FALSE  FALSE   TRUE
#> [2,]      FALSE  TRUE   TRUE  FALSE  FALSE
#> [3,]      FALSE FALSE   TRUE  FALSE   TRUE
#> [4,]      FALSE  TRUE   TRUE  FALSE  FALSE
#> [5,]      FALSE FALSE   TRUE   TRUE  FALSE
# One-hot, custom names, regex matched, logical output:
logic_map(x, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE, logical.out = TRUE)
#>      alpha  beta delta
#> [1,] FALSE  TRUE  TRUE
#> [2,] FALSE FALSE  TRUE
#> [3,] FALSE  TRUE  TRUE
#> [4,] FALSE FALSE  TRUE
#> [5,] FALSE FALSE  TRUE
# One-hot, valued, custom names, regex matched, numeric output:
logic_map(x, avec = v, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE)
#>      alpha beta delta
#> [1,]     0   34    34
#> [2,]     0    0    49
#> [3,]     0   37    37
#> [4,]     0    0    48
#> [5,]     0    0    36