Skip to contents

Collapse a binary indicator matrix into a character vector of labels. For each row, the labels of all columns equal to 1 are pasted together, comma-separated. This is the inverse of multi-hot encoding: unlike one_hot2factor, which assumes a single 1 per row, %BC% supports rows with multiple 1s (multi-label data, e.g. multiple-choice survey responses, tags, or set membership). Rows of all zeros return NA.

Usage

x %BC% labels

Arguments

x

A binary matrix or data.frame

labels

Character vector length equal to ncol(x)

Value

a character vector

Author

EDG

Examples

# Multi-hot matrix: each row can belong to multiple categories
x <- rbind(
  c(1, 0, 1),
  c(0, 1, 0),
  c(1, 1, 1),
  c(0, 0, 0)
)
labels <- c("apple", "banana", "cherry")
x %BC% labels
#> [1] "apple,cherry"        "banana"              "apple,banana,cherry"
#> [4] NA                   
# -> "apple,cherry", "banana", "apple,banana,cherry", NA