Skip to content
Prev 57858 / 63424 Next

A weird behaviour of strsplit?

The fact that strsplit() doesn't say anything about 'split' being longer 
than 'x' adds to the confusion:

   > strsplit(c("xAy", "xxByB", "xCyCCz"), split=c("A", "B", "C", "D"))
   [[1]]
   [1] "x" "y"

   [[2]]
   [1] "xx" "y"

   [[3]]
   [1] "x" "y" ""  "z"

A warning (or error) would go a long way in helping the user realize 
they're doing something wrong.

No warning either when 'split' is shorter than 'x' but the length of the 
latter is not a multiple of the length of the former:

   > strsplit(c("xAy", "xxByB", "xCyCCz"), split=c("A", "B"))
   [[1]]
   [1] "x" "y"

   [[2]]
   [1] "xx" "y"

   [[3]]
   [1] "xCyCCz"

Which is also unexpected given that most binary operations do issue a 
warning in this case (e.g. 11:13 * 1:2).

H.
On 12/18/19 06:48, Duncan Murdoch wrote: