https://github.com/cran/data.table
Raw File
Tip revision: 12e9a703797a68d769e8795d717cc818a260718f authored by Matt Dowle on 19 October 2020, 17:50:02 UTC
version 1.13.2
Tip revision: 12e9a70
chmatch.Rd
\name{chmatch}
\alias{chmatch}
\alias{\%chin\%}
\alias{chorder}
\alias{chgroup}
\title{ Faster match of character vectors }
\description{
  \code{chmatch} returns a vector of the positions of (first) matches of its first argument in its second. Both arguments must be character vectors.

  \code{\%chin\%} is like \code{\%in\%}, but for character vectors.
}
\usage{
chmatch(x, table, nomatch=NA_integer_)
x \%chin\% table
chorder(x)
chgroup(x)
}
\arguments{
  \item{x}{ character vector: the values to be matched, or the values to be ordered or grouped }
  \item{table}{ character vector: the values to be matched against. }
  \item{nomatch}{ the value to be returned in the case when no match is found. Note that it is coerced to integer. }
}
\details{
  Fast versions of \code{match}, \code{\%in\%} and \code{order}, optimised for character vectors. \code{chgroup} groups together duplicated values but retains the group order (according the first appearance order of each group), efficiently. They have been primarily developed for internal use by data.table, but have been exposed since that seemed appropriate.

  Strings are already cached internally by R (\code{CHARSXP}) and that is utilised by these functions. No hash table is built or cached, so the first call is the same speed as subsequent calls. Essentially, a counting sort (similar to \code{base::sort.list(x,method="radix")}, see \code{\link{setkey}}) is implemented using the (almost) unused truelength of CHARSXP as the counter. \emph{Where} R \emph{has} used truelength of CHARSXP (where a character value is shared by a variable name), the non zero truelengths are stored first and reinstated afterwards. Each of the \code{ch*} functions implements a variation on this theme. Remember that internally in R, length of a CHARSXP is the nchar of the string and DATAPTR is the string itself.

  Methods that do build and cache a hash table (such as the \href{https://cran.r-project.org/package=fastmatch}{fastmatch package}) are \emph{much} faster on subsequent calls (almost instant) but a little slower on the first. Therefore \code{chmatch} may be particularly suitable for ephemeral vectors (such as local variables in functions) or tasks that are only done once. Much depends on the length of \code{x} and \code{table}, how many unique strings each contains, and whether the position of the first match is all that is required.

  It may be possible to speed up fastmatch's hash table build time by using the technique in \code{data.table}, and we have suggested this to its author. If successful, fastmatch would then be fastest in all cases.
  }
\value{
    As \code{match} and \code{\%in\%}. \code{chorder} and \code{chgroup} return an integer index vector.
}
\seealso{ \code{\link[base]{match}}, \code{\link{\%in\%}}
}
\note{ The name \code{charmatch} was taken by \code{\link[base]{charmatch}}, hence \code{chmatch}.
}
\examples{
# Please type 'example(chmatch)' to run this and see timings on your machine

N = 1e5
# N is set small here (1e5) to reduce runtime because every day CRAN runs and checks
# all documentation examples in addition to the package's test suite.
# The comments here apply when N has been changed to 1e8 and were run on 2018-05-13
# with R 3.5.0 and data.table 1.11.2.

u = as.character(as.hexmode(1:10000))
y = sample(u,N,replace=TRUE)
x = sample(u)
                                           #  With N=1e8 ...
system.time(a <- match(x,y))               #  4.6s
system.time(b <- chmatch(x,y))             #  1.8s
identical(a,b)

system.time(a <- x \%in\% y)               #  4.5s
system.time(b <- x \%chin\% y)             #  1.7s
identical(a,b)

# Different example with more unique strings ...
u = as.character(as.hexmode(1:(N/10)))
y = sample(u,N,replace=TRUE)
x = sample(u,N,replace=TRUE)
system.time(a <- match(x,y))               # 46s
system.time(b <- chmatch(x,y))             # 16s
identical(a,b)
}
\keyword{ data }
back to top