#' Build conceptual scale based on pattern matching
#'
#' @example inst/examples/make-scale-regex.r
#' @param objs Vector (usually character or factor).
#' @param patterns Character vector of regular expressions. Each attribute of
#' the resulting context amounts to \code{grepl(x = objs, pattern = pattern)}
#' for some \code{pattern} in \code{patterns}.
#' @param split Character. Regular expression used to split \code{objs} into
#' \code{patterns}, if \code{patterns} is not provided.
#' @return A binary matrix (formal context).
#' @export
make_scale_regex <- function(objs, patterns, split) {
# each object should only appear once (in order of appearance)
objs <- unique(objs)
# use patterns if present; otherwise split
if (missing(patterns)) {
patterns <- unique(unlist(strsplit(objs, split = split)))
} else {
if (!missing(split)) warning("'patterns' provided; ignoring 'split")
}
# conceptual scale based on logical grep along patterns
scale_mat <- sapply(patterns, grepl, x = objs)
class(scale_mat) <- "numeric"
rownames(scale_mat) <- objs
scale_mat
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.