[Rd] A weird behaviour of strsplit?
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Wed Dec 18 15:48:59 CET 2019
On 18/12/2019 9:42 a.m., IAGO GINÉ VÁZQUEZ wrote:
> Hi all,
>
> In the help of strsplit one can read
>
> split character vector (or object which can be coerced to such) containing regular expression<http://127.0.0.1:39783/help/library/base/help/regular%20expression>(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters. Ifsplit has length greater than 1, it is re-cycled along x.
>
> Taking into account that split is said to be a vector (not a length 1 vector) and the last claim above, I would expect that the output of
>
>
> strsplit("3:4", split = c(",",":"), fixed = TRUE)
>
> was the same than the output of
>
> strsplit("3:4", split = c(":"), fixed = TRUE)
>
> that is, splitting by "," (without effect in this example) and also by ":"
>
> [[1]]
> [1] "3" "4"
>
> But, instead, I get
> [[1]]
> [1] "3:4"
>
> Am I wrongly understanding the help? Is it an expected output?
> I tried with R 3.6.1 for Windows (10).
Yes, you are misunderstanding the help. Your input x has length 1, so
only the first element of split will be used. If you wanted to use
both, you would need a longer x. For example,
> strsplit(c("1:2", "3:4"), split=c(",", ":"), fixed=TRUE)
[[1]]
[1] "1:2"
[[2]]
[1] "3" "4"
The first element is split using "," -- since there are none, there's no
splitting done. The second element is split using ":".
Duncan Murdoch
More information about the R-devel
mailing list