From henr|k@bengt@@on @end|ng |rom gm@||@com  Thu Mar  3 20:27:11 2022
From: henr|k@bengt@@on @end|ng |rom gm@||@com (Henrik Bengtsson)
Date: Thu, 3 Mar 2022 11:27:11 -0800
Subject: [Rd] strsplit() and final empty values
In-Reply-To: <CAC0O-5dYgPxHmaBsbyiW9-aXjjAqQ7Wn16FF2N+LsHiyw0efhA@mail.gmail.com>
References: <CAC0O-5f1U2m8ji=4UMWNBFwW7DFU72L1eNyo6nJN0sBpbcCPdw@mail.gmail.com>
 <CAFDcVCTPUqygyNHsKJYp=UAGhFcjOigrtsjno-=J+9T7=dLN8w@mail.gmail.com>
 <CAC0O-5dYgPxHmaBsbyiW9-aXjjAqQ7Wn16FF2N+LsHiyw0efhA@mail.gmail.com>
Message-ID: <CAFDcVCSj-i012Fhtkpmk2bNfCu7+4C-JnvJxvD=upsmHJQM7sA@mail.gmail.com>

On Thu, Mar 3, 2022 at 12:58 AM Dzmitry Batrakou <d.batrakou at gmail.com> wrote:
>
> Alternatively, the man page for the strsplit() could include the following (or a better phrased version):
>
> "
> Note that the trailing empty values will be trimmed (see penultimate example). A way around this is to use
> > read.table(text = paste(strings.to.split, collapse = "\n"), sep = split)

Another workaround is to append another split symbol (or string) to
the end of the string you're splitting, e.g.

> strsplit(paste0("a:b:c:", split <- ":"), split = split, fixed = TRUE)
[[1]]
[1] "a" "b" "c" ""

/Henrik

> "
>
> Dzmitry
>
> On Thu, 3 Mar 2022 at 04:13, Henrik Bengtsson <henrik.bengtsson at gmail.com> wrote:
>>
>> Here's an example clarifying the issue:
>>
>> > strsplit("a:b:c:d", split = ":", fixed = TRUE)
>> [[1]]
>> [1] "a" "b" "c" "d"
>>
>> > strsplit("a:b:c:", split = ":", fixed = TRUE)
>> [[1]]
>> [1] "a" "b" "c"
>>
>> I also ran into this a few times, and I agree that this complicated
>> things when you need to preserve that last empty element.  Instead of
>> changing the default behavior, which would probably break lots of
>> existing code relying on it, one could introduce a new,
>> backward-compatible argument `drop = TRUE`, e.g.
>>
>> > strsplit("a:b:c:", split = ":", fixed = TRUE, drop = FALSE)
>> [[1]]
>> [1] "a" "b" "c" ""
>>
>> My $.02
>>
>> /Henrik
>>
>>
>>
>> On Sat, Feb 26, 2022 at 6:39 AM Dzmitry Batrakou <d.batrakou at gmail.com> wrote:
>> >
>> > Hello,
>> >
>> > I would like to suggest changing the behaviour of the strsplit() function
>> > with multiple trailing empty values. Currently, `strsplit(x = 'value::',
>> > split = ':')` produces a list of length 2 ('value',''). This behaviour is
>> > documented in the manual (penultimate example), however, I would argue, is
>> > illogical and can lead to unexpected parsing results. One example is
>> > splitting delimited value strings into a table.
>> >
>> > Regards,
>> > Dzmitry
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-devel at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] strsplit() and final empty values
Henrik Bengtsson