Examples for 'data.table::chmatch'


Faster match of character vectors

Aliases: chmatch %chin% chorder chgroup

Keywords: data

### ** 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
       user      system     elapsed 
0.002000000 0.001000000 0.002999999 
system.time(b <- chmatch(x,y))             #  1.8s
       user      system     elapsed 
0.001000000 0.001000000 0.001000002 
identical(a,b)
[1] TRUE
system.time(a <- x %in% y)               #  4.5s
       user      system     elapsed 
0.002000000 0.000000000 0.002999999 
system.time(b <- x %chin% y)             #  1.7s
       user      system     elapsed 
0.000000000 0.000000000 0.001000002 
identical(a,b)
[1] TRUE
# 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
       user      system     elapsed 
0.005000000 0.000000000 0.004000001 
system.time(b <- chmatch(x,y))             # 16s
        user       system      elapsed 
0.0020000000 0.0000000000 0.0009999983 
identical(a,b)
[1] TRUE

[Package data.table version 1.14.2 Index]