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,
  sparse = TRUE,
  progress = 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 stri_detect_regex.

sparse

(logical | TRUE) When FALSE, the result is coerced into a matrix

progress

(logical | TRUE) When TRUE, a progress bar is shown in the console

...

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

Value

A (sparse) matrix, the column names being the 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

# :: Plain vector input:
x <- LETTERS[1:10]
logic_map(x)
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#>   [[ suppressing 10 column names ‘A’, ‘B’, ‘C’ ... ]]
#>                          
#>  [1,] 1 . . . . . . . . .
#>  [2,] . 1 . . . . . . . .
#>  [3,] . . 1 . . . . . . .
#>  [4,] . . . 1 . . . . . .
#>  [5,] . . . . 1 . . . . .
#>  [6,] . . . . . 1 . . . .
#>  [7,] . . . . . . 1 . . .
#>  [8,] . . . . . . . 1 . .
#>  [9,] . . . . . . . . 1 .
#> [10,] . . . . . . . . . 1

# :: Flavored vector input:
k <- c("steelblue2", "tan3", "gray47", "gray27", "gray60")
x <- replicate(50, sample(k, 1), simplify = TRUE)
v <- sample(100, length(x), TRUE)

ls.str(pattern = "(x|k|v)$")
#> k :  chr [1:5] "steelblue2" "tan3" "gray47" "gray27" "gray60"
#> v :  int [1:50] 58 29 74 67 100 14 66 69 82 37 ...
#> x :  chr [1:50] "gray60" "steelblue2" "steelblue2" "gray60" "tan3" "gray60" ...

# One-hot, default names:
logic_map(x)
#> 50 x 5 sparse Matrix of class "dgCMatrix"
#>       gray27 gray47 gray60 steelblue2 tan3
#>  [1,]      .      .      1          .    .
#>  [2,]      .      .      .          1    .
#>  [3,]      .      .      .          1    .
#>  [4,]      .      .      1          .    .
#>  [5,]      .      .      .          .    1
#>  [6,]      .      .      1          .    .
#>  [7,]      .      1      .          .    .
#>  [8,]      .      .      .          1    .
#>  [9,]      1      .      .          .    .
#> [10,]      .      .      .          .    1
#> [11,]      .      .      .          1    .
#> [12,]      .      1      .          .    .
#> [13,]      .      .      .          1    .
#> [14,]      .      1      .          .    .
#> [15,]      .      .      1          .    .
#> [16,]      .      .      1          .    .
#> [17,]      .      .      1          .    .
#> [18,]      .      .      .          1    .
#> [19,]      .      .      1          .    .
#> [20,]      .      1      .          .    .
#> [21,]      .      .      1          .    .
#> [22,]      .      .      .          .    1
#> [23,]      1      .      .          .    .
#> [24,]      .      1      .          .    .
#> [25,]      1      .      .          .    .
#> [26,]      .      .      .          .    1
#> [27,]      .      .      .          1    .
#> [28,]      .      .      .          .    1
#> [29,]      .      1      .          .    .
#> [30,]      1      .      .          .    .
#> [31,]      .      1      .          .    .
#> [32,]      .      .      .          1    .
#> [33,]      .      .      .          .    1
#> [34,]      1      .      .          .    .
#> [35,]      .      .      1          .    .
#> [36,]      .      .      1          .    .
#> [37,]      1      .      .          .    .
#> [38,]      .      .      .          1    .
#> [39,]      .      1      .          .    .
#> [40,]      .      .      .          1    .
#> [41,]      .      1      .          .    .
#> [42,]      1      .      .          .    .
#> [43,]      .      1      .          .    .
#> [44,]      .      .      .          .    1
#> [45,]      .      .      .          1    .
#> [46,]      1      .      .          .    .
#> [47,]      1      .      .          .    .
#> [48,]      .      .      .          .    1
#> [49,]      1      .      .          .    .
#> [50,]      .      .      1          .    .

# One-hot, custom names:
logic_map(fvec = x, bvec = k)
#> 50 x 5 sparse Matrix of class "dgCMatrix"
#>       steelblue2 tan3 gray47 gray27 gray60
#>  [1,]          .    .      .      .      1
#>  [2,]          1    .      .      .      .
#>  [3,]          1    .      .      .      .
#>  [4,]          .    .      .      .      1
#>  [5,]          .    1      .      .      .
#>  [6,]          .    .      .      .      1
#>  [7,]          .    .      1      .      .
#>  [8,]          1    .      .      .      .
#>  [9,]          .    .      .      1      .
#> [10,]          .    1      .      .      .
#> [11,]          1    .      .      .      .
#> [12,]          .    .      1      .      .
#> [13,]          1    .      .      .      .
#> [14,]          .    .      1      .      .
#> [15,]          .    .      .      .      1
#> [16,]          .    .      .      .      1
#> [17,]          .    .      .      .      1
#> [18,]          1    .      .      .      .
#> [19,]          .    .      .      .      1
#> [20,]          .    .      1      .      .
#> [21,]          .    .      .      .      1
#> [22,]          .    1      .      .      .
#> [23,]          .    .      .      1      .
#> [24,]          .    .      1      .      .
#> [25,]          .    .      .      1      .
#> [26,]          .    1      .      .      .
#> [27,]          1    .      .      .      .
#> [28,]          .    1      .      .      .
#> [29,]          .    .      1      .      .
#> [30,]          .    .      .      1      .
#> [31,]          .    .      1      .      .
#> [32,]          1    .      .      .      .
#> [33,]          .    1      .      .      .
#> [34,]          .    .      .      1      .
#> [35,]          .    .      .      .      1
#> [36,]          .    .      .      .      1
#> [37,]          .    .      .      1      .
#> [38,]          1    .      .      .      .
#> [39,]          .    .      1      .      .
#> [40,]          1    .      .      .      .
#> [41,]          .    .      1      .      .
#> [42,]          .    .      .      1      .
#> [43,]          .    .      1      .      .
#> [44,]          .    1      .      .      .
#> [45,]          1    .      .      .      .
#> [46,]          .    .      .      1      .
#> [47,]          .    .      .      1      .
#> [48,]          .    1      .      .      .
#> [49,]          .    .      .      1      .
#> [50,]          .    .      .      .      1

# One-hot, custom names, logical output:
logic_map(fvec = x, bvec = k, logical.out = TRUE)
#> 50 x 5 sparse Matrix of class "ngCMatrix"
#>       steelblue2 tan3 gray47 gray27 gray60
#>  [1,]          .    .      .      .      |
#>  [2,]          |    .      .      .      .
#>  [3,]          |    .      .      .      .
#>  [4,]          .    .      .      .      |
#>  [5,]          .    |      .      .      .
#>  [6,]          .    .      .      .      |
#>  [7,]          .    .      |      .      .
#>  [8,]          |    .      .      .      .
#>  [9,]          .    .      .      |      .
#> [10,]          .    |      .      .      .
#> [11,]          |    .      .      .      .
#> [12,]          .    .      |      .      .
#> [13,]          |    .      .      .      .
#> [14,]          .    .      |      .      .
#> [15,]          .    .      .      .      |
#> [16,]          .    .      .      .      |
#> [17,]          .    .      .      .      |
#> [18,]          |    .      .      .      .
#> [19,]          .    .      .      .      |
#> [20,]          .    .      |      .      .
#> [21,]          .    .      .      .      |
#> [22,]          .    |      .      .      .
#> [23,]          .    .      .      |      .
#> [24,]          .    .      |      .      .
#> [25,]          .    .      .      |      .
#> [26,]          .    |      .      .      .
#> [27,]          |    .      .      .      .
#> [28,]          .    |      .      .      .
#> [29,]          .    .      |      .      .
#> [30,]          .    .      .      |      .
#> [31,]          .    .      |      .      .
#> [32,]          |    .      .      .      .
#> [33,]          .    |      .      .      .
#> [34,]          .    .      .      |      .
#> [35,]          .    .      .      .      |
#> [36,]          .    .      .      .      |
#> [37,]          .    .      .      |      .
#> [38,]          |    .      .      .      .
#> [39,]          .    .      |      .      .
#> [40,]          |    .      .      .      .
#> [41,]          .    .      |      .      .
#> [42,]          .    .      .      |      .
#> [43,]          .    .      |      .      .
#> [44,]          .    |      .      .      .
#> [45,]          |    .      .      .      .
#> [46,]          .    .      .      |      .
#> [47,]          .    .      .      |      .
#> [48,]          .    |      .      .      .
#> [49,]          .    .      .      |      .
#> [50,]          .    .      .      .      |

# One-hot, custom names, regex matched, logical output:
logic_map(fvec = x, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE, logical.out = TRUE)
#> 50 x 3 sparse Matrix of class "ngCMatrix"
#>       alpha beta delta
#>  [1,]     .    |     |
#>  [2,]     |    .     .
#>  [3,]     |    .     .
#>  [4,]     .    |     |
#>  [5,]     .    .     .
#>  [6,]     .    |     |
#>  [7,]     .    .     |
#>  [8,]     |    .     .
#>  [9,]     .    .     |
#> [10,]     .    .     .
#> [11,]     |    .     .
#> [12,]     .    .     |
#> [13,]     |    .     .
#> [14,]     .    .     |
#> [15,]     .    |     |
#> [16,]     .    |     |
#> [17,]     .    |     |
#> [18,]     |    .     .
#> [19,]     .    |     |
#> [20,]     .    .     |
#> [21,]     .    |     |
#> [22,]     .    .     .
#> [23,]     .    .     |
#> [24,]     .    .     |
#> [25,]     .    .     |
#> [26,]     .    .     .
#> [27,]     |    .     .
#> [28,]     .    .     .
#> [29,]     .    .     |
#> [30,]     .    .     |
#> [31,]     .    .     |
#> [32,]     |    .     .
#> [33,]     .    .     .
#> [34,]     .    .     |
#> [35,]     .    |     |
#> [36,]     .    |     |
#> [37,]     .    .     |
#> [38,]     |    .     .
#> [39,]     .    .     |
#> [40,]     |    .     .
#> [41,]     .    .     |
#> [42,]     .    .     |
#> [43,]     .    .     |
#> [44,]     .    .     .
#> [45,]     |    .     .
#> [46,]     .    .     |
#> [47,]     .    .     |
#> [48,]     .    .     .
#> [49,]     .    .     |
#> [50,]     .    |     |

# One-hot, valued, custom names, regex matched, numeric output:
logic_map(fvec = x, avec = v, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE)
#> 50 x 3 sparse Matrix of class "dgCMatrix"
#>       alpha beta delta
#>  [1,]     .   58    58
#>  [2,]    29    .     .
#>  [3,]    74    .     .
#>  [4,]     .   67    67
#>  [5,]     .    .     .
#>  [6,]     .   14    14
#>  [7,]     .    .    66
#>  [8,]    69    .     .
#>  [9,]     .    .    82
#> [10,]     .    .     .
#> [11,]     4    .     .
#> [12,]     .    .    83
#> [13,]    56    .     .
#> [14,]     .    .    52
#> [15,]     .   57    57
#> [16,]     .   17    17
#> [17,]     .   87    87
#> [18,]    31    .     .
#> [19,]     .   93    93
#> [20,]     .    .    52
#> [21,]     .   74    74
#> [22,]     .    .     .
#> [23,]     .    .    55
#> [24,]     .    .    73
#> [25,]     .    .    89
#> [26,]     .    .     .
#> [27,]    73    .     .
#> [28,]     .    .     .
#> [29,]     .    .    89
#> [30,]     .    .    51
#> [31,]     .    .    29
#> [32,]    49    .     .
#> [33,]     .    .     .
#> [34,]     .    .    63
#> [35,]     .   23    23
#> [36,]     .   57    57
#> [37,]     .    .    31
#> [38,]    59    .     .
#> [39,]     .    .    75
#> [40,]    54    .     .
#> [41,]     .    .    40
#> [42,]     .    .    60
#> [43,]     .    .     7
#> [44,]     .    .     .
#> [45,]    92    .     .
#> [46,]     .    .    12
#> [47,]     .    .     3
#> [48,]     .    .     .
#> [49,]     .    .    72
#> [50,]     .   12    12

# One-hot, valued, custom names, regex matched, numeric output, non-sparse:
logic_map(fvec = x, avec = v, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE, sparse = FALSE)
#>       alpha beta delta
#>  [1,]     0   58    58
#>  [2,]    29    0     0
#>  [3,]    74    0     0
#>  [4,]     0   67    67
#>  [5,]     0    0     0
#>  [6,]     0   14    14
#>  [7,]     0    0    66
#>  [8,]    69    0     0
#>  [9,]     0    0    82
#> [10,]     0    0     0
#> [11,]     4    0     0
#> [12,]     0    0    83
#> [13,]    56    0     0
#> [14,]     0    0    52
#> [15,]     0   57    57
#> [16,]     0   17    17
#> [17,]     0   87    87
#> [18,]    31    0     0
#> [19,]     0   93    93
#> [20,]     0    0    52
#> [21,]     0   74    74
#> [22,]     0    0     0
#> [23,]     0    0    55
#> [24,]     0    0    73
#> [25,]     0    0    89
#> [26,]     0    0     0
#> [27,]    73    0     0
#> [28,]     0    0     0
#> [29,]     0    0    89
#> [30,]     0    0    51
#> [31,]     0    0    29
#> [32,]    49    0     0
#> [33,]     0    0     0
#> [34,]     0    0    63
#> [35,]     0   23    23
#> [36,]     0   57    57
#> [37,]     0    0    31
#> [38,]    59    0     0
#> [39,]     0    0    75
#> [40,]    54    0     0
#> [41,]     0    0    40
#> [42,]     0    0    60
#> [43,]     0    0     7
#> [44,]     0    0     0
#> [45,]    92    0     0
#> [46,]     0    0    12
#> [47,]     0    0     3
#> [48,]     0    0     0
#> [49,]     0    0    72
#> [50,]     0   12    12

# :: Flavored list input:
x <- replicate(50, sample(k, 2) |> sort(), simplify = FALSE)

str(head(x, 10)) 
#> List of 10
#>  $ : chr [1:2] "gray47" "tan3"
#>  $ : chr [1:2] "gray60" "tan3"
#>  $ : chr [1:2] "gray27" "steelblue2"
#>  $ : chr [1:2] "gray60" "tan3"
#>  $ : chr [1:2] "steelblue2" "tan3"
#>  $ : chr [1:2] "gray47" "steelblue2"
#>  $ : chr [1:2] "gray27" "steelblue2"
#>  $ : chr [1:2] "gray27" "gray47"
#>  $ : chr [1:2] "gray27" "gray47"
#>  $ : chr [1:2] "gray27" "gray47"

logic_map(fvec = x, avec = v, bvec = c(alpha = "blue", beta = 60, delta = "gray"), regex = TRUE)
#> 50 x 3 sparse Matrix of class "dgCMatrix"
#>       alpha beta delta
#>  [1,]     .    .    58
#>  [2,]     .   29    29
#>  [3,]    74    .    74
#>  [4,]     .   67    67
#>  [5,]   100    .     .
#>  [6,]    14    .    14
#>  [7,]    66    .    66
#>  [8,]     .    .    69
#>  [9,]     .    .    82
#> [10,]     .    .    37
#> [11,]     4    .     4
#> [12,]    83   83    83
#> [13,]     .   56    56
#> [14,]     .   52    52
#> [15,]     .    .    57
#> [16,]     .    .    17
#> [17,]     .    .    87
#> [18,]     .   31    31
#> [19,]     .   93    93
#> [20,]     .   52    52
#> [21,]     .    .    74
#> [22,]    24    .     .
#> [23,]     .   55    55
#> [24,]    73    .    73
#> [25,]     .    .    89
#> [26,]     .   10    10
#> [27,]     .    .    73
#> [28,]    83    .     .
#> [29,]     .    .    89
#> [30,]     .   51    51
#> [31,]     .    .    29
#> [32,]    49   49    49
#> [33,]    79   79    79
#> [34,]     .    .    63
#> [35,]     .   23    23
#> [36,]     .    .    57
#> [37,]    31    .    31
#> [38,]    59   59    59
#> [39,]     .   75    75
#> [40,]    54    .    54
#> [41,]     .   40    40
#> [42,]     .    .    60
#> [43,]     .    .     7
#> [44,]     .   51    51
#> [45,]     .   92    92
#> [46,]     .   12    12
#> [47,]     .    3     3
#> [48,]     .    .    16
#> [49,]     .   72    72
#> [50,]     .   12    12