Bug 15311 - 'regmatches<-' issues warning and produces incorrect results
Summary: 'regmatches<-' issues warning and produces incorrect results
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R 3.0.0
Hardware: Other Linux-Ubuntu
: P5 normal
Assignee: R-core
Depends on:
Reported: 2013-05-14 14:59 UTC by Stephen Weston
Modified: 2013-05-14 17:35 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Stephen Weston 2013-05-14 14:59:51 UTC

  The 'regmatches<-' function issues a warning and produces incorrect results when called with a vector with length > 1 where at least one element does not match the regular expression.  Specifically, this happens when the object with the match data (argument 'm') was created with 'regexpr' (it seems to work with 'gregexpr').

Steps to reproduce:

  x <- c('1', 'B', '3')
  m <- regexpr('\\d', x)
  regmatches(x, m) <- c('A', 'C')

Actual results:

  Warning message:
  In `regmatches<-`(`*tmp*`, m, value = c("A", "C")) :
    number of items to replace is not a multiple of replacement length
  > print(x)
  [1] "A"    "B"    "BCNA"

Expected results:

  > print(x)
  [1] "A" "B" "C"

Build date and platform:

  I've seen this problem in 2.15.X versions of R, but it also seems
  to be in the latest release candidate of 3.0.1:

  R version 3.0.1 RC (2013-05-12 r62739) -- "Good Sport"
  Platform: x86_64-unknown-linux-gnu (64-bit)
Comment 1 Duncan Murdoch 2013-05-14 17:35:57 UTC
There's a simple error in the code that handled this case.  I'll fix it in R-devel, and after 3.0.1 is released, port the fix to R-patched.