I am trying to create a case-insensitive %in%
like function. I have so far created this:
qs <- function(str, vec, na.rm = TRUE) {
stopifnot("str must have length 1" = length(str) == 1)
if(na.rm) {
any(stringi::stri_detect_fixed(str, na.omit(vec), max_count = 1, opts_fixed = list(case_insensitive = TRUE)))
} else {
any(stringi::stri_detect_fixed(str, vec, max_count = 1, opts_fixed = list(case_insensitive = TRUE)))
}
For my use case, I need this vectorized, so I can do:
vecqs <- Vectorize(qs, "str")
However, I have read that Vectorize
is rather slow. I have also been reading about data.table::chmatch
and the fastmatch
package. Both of these implement their own %in%
type function ( chmatch
for data.table
). These would be great but I don't know how to make chmatch
case insensitive.
Based on the comments above, I came to the relatively simple solution below:
#' Quick Search
#'
#' Quick case-insensitive search of strings in a character vector
#'
#' @param str a character vector: the values to be matched
#' @param vec a character vector: the values to be matched against
#'
#' @details Utilizes \code{data.table::`%chin%`} to rapidly complete a case-insensitive search
#' through a character vector to return a logical vector of string detections.
#' Will always return TRUE or FALSE for each position of \code{str} regardless of NA missing values
#' in either provided vector. NA in \code{str} will never match an NA value in \code{vec}.
#'
#' @return a logical vector of length \code{length(str)}
#'
#' @export
#' @importFrom data.table %chin%
#'
#' @examples
#' x <- c("apple","banana","cherry",NA)
#' "apple" %qsin% x
#' c("APPLE","BANANA","coconut", NA) %qsin% x
#'
`%qsin%` <- function(str, vec) {
tolower(str) %chin% na.omit(tolower(vec))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.