[Rd] pmatch bug?

François Rousset |r@nco|@@rou@@et @end|ng |rom umontpe|||er@|r
Mon Jun 15 21:12:29 CEST 2026


Dear R-devel list,

the following code shows NA's appearing in the result of pmatch() when 
comparing a vector to itself when the length of the vector is more than 100.
The main specificity of this example is that elements are repeated in 
the vector which is matched to itself.
The results when they do not include any NA (i.e. for argument of length 
<=100) are exactly as I expect from the documentation.
I see that the source C code uses distinct algorithms whether n_input <= 
100 || n_target <= 100 or not. Could there by a problem in the source 
code for larger values ?
The NA's correspond to the first positions of the second replicate of 
the integer sequence, e.g. positions 52 to 56 if n=51 in the example below.

But as shown below, the NA's do not appear when the sequence is reversed 
before being compared to itself.

This was first detected with R 4.5.3 and is reproducible with a 
just-downloaded, virgin R-devel installation.

Thanks in advance for any feedback,

F.

======================

countNAs <- function(n, rev.=FALSE) {
   seqn <- seq(n)
   if (rev.) seqn <- rev(seqn)
   seqn <- rep(seqn,2)                         # of  length 2 n: NA's 
appear when 2 n > 100
   chk <- pmatch(seqn, seqn)             # I expect the result to be 
seq(2*n)
   sum(is.na(chk))
}

sapply(1:100, countNAs )
sapply(1:100, countNAs , rev.=TRUE)

=======================

Results:
 > sapply(1:100, countNAs )
   [1]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 0  0  
0  0  0  0  0  0
  [29]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 0  0  
5  5  5  5  5  5
  [57]  5  5  5  6  6  6  6  6  6  6  6  6  6  7  7  7  7  7  7  7 7  7  
7  8  8  8  8  8
  [85]  8  8  8  8  8  9  9  9  9  9  9  9  9  9  9 10
 > sapply(1:100, countNAs , rev.=TRUE)
   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0
  [43] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0
  [85] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 > sessionInfo()
R Under development (unstable) (2026-06-14 r90150 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default
   LAPACK version 3.12.1

locale:
[1] LC_COLLATE=French_France.utf8  LC_CTYPE=French_France.utf8
[3] LC_MONETARY=French_France.utf8 LC_NUMERIC=C
[5] LC_TIME=French_France.utf8

time zone: Europe/Paris
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  base

loaded via a namespace (and not attached):
[1] compiler_4.7.0 tools_4.7.0



More information about the R-devel mailing list