[R] sub('^', .....) bugs

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Wed 23 Mar 2005 - 18:55:38 EST

>>>>> "David" == David Forrest <drf5n@maplepark.com> >>>>> on Tue, 22 Mar 2005 15:02:20 -0600 (CST) writes:

    David> According to help(sub), the ^ should match the     David> zero-length string at the beginning of a string:

yes, indeed.

    David> sub('^','var',1:3) # "1" "2" "3"     David> sub('$','var',1:3) # "1var" "2var" "3var"

    David> # This generates what I expected from the first case:     David> sub('^.','var',11:13) # "var1" "var2" "var3"

there are even more fishy things here:

  1. In your cases, the integer 'x' argument is auto-coerced to character, however that fails as soon as 'perl = TRUE' is used.

 > sub('^','v_', 1:3, perl=TRUE)
 Error in sub.perl(pattern, replacement, x, ignore.case) :

         invalid argument

 {one can argue that this is not a bug, since the help file asks   for 'x' to be a character vector; OTOH, we have   as.character(.) magic in many other places, i.e. quite   naturally here;
  at least perl=TRUE and perl=FALSE should behave consistently.}

2) The 'perl=TRUE' case behaves even more problematically here:

> sub('^','v_', LETTERS[1:3], perl=TRUE)
  [1] "A\0e" "B\0J" "C\0S"
> sub('^','v_', LETTERS[1:3], perl=TRUE)
  [1] "A\0J" "B\0P" "C\0J"
> sub('^','v_', LETTERS[1:3], perl=TRUE)
  [1] "A\0\0" "B\0\0" "C\0m"
>

 i.e., the result is random nonsense.

Note that this happens both for R-patched (2.0.1) and R-devel (2.1.0 alpha).

==> "forwarded" as bug report to R-bugs



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Mar 23 19:02:11 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:55 EST