From i.ucar86 at gmail.com  Fri Oct 20 21:49:58 2017
From: i.ucar86 at gmail.com (=?UTF-8?B?ScOxYWtpIMOaY2Fy?=)
Date: Fri, 20 Oct 2017 21:49:58 +0200
Subject: [Rd] split() - unexpected sorting of results
In-Reply-To: <CAB-BFRp=_kmi5UaGESvzJzh6dxKz891LkFWY+nJ2uya=fQpZ7A@mail.gmail.com>
References: <CAB-BFRp=_kmi5UaGESvzJzh6dxKz891LkFWY+nJ2uya=fQpZ7A@mail.gmail.com>
Message-ID: <CALEXWq2Ycj2-Ko1jBoUPG7TsV7EAi2+2nsUcNJ6gwAqPKqXySg@mail.gmail.com>

Hi Peter,

2017-10-20 21:33 GMT+02:00 Peter Meissner <retep.meissner at gmail.com>:
> Hey,
>
> I found this - for me - quite surprising and puzzling behaviour of split().
>
>
> split(1:11, as.character(1:11))
> split(1:11, 1:11)
>
>
> When splitting by numerics everything works as expected - sorting of input
> == sorting of output -- but when using a character vector everything gets
> re-sorted alphabetical.
>
>
> Although, there are some references in the help files to what happens when
> using split, I did not find any note on this - for me - rather unexpected
> behaviour.

As the documentation states,

       f: a ?factor? in the sense that ?as.factor(f)? defines the
          grouping, or a list of such factors in which case their
          interaction is used for the grouping.

And, in fact,

> as.factor(1:11)
 [1] 1  2  3  4  5  6  7  8  9  10 11
Levels: 1 2 3 4 5 6 7 8 9 10 11

> as.factor(as.character(1:11))
 [1] 1  2  3  4  5  6  7  8  9  10 11
Levels: 1 10 11 2 3 4 5 6 7 8 9

Regards,
I?aki

> I would like it best when the sorting of split results stays the same no
> matter the input (sorting of input == sorting of output)
>
> If that is not possibly a note of caution in the help pages and maybe an
> example might be valuable.
>
>
> Best, Peter
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] split() - unexpected sorting of results
=?UTF-8?B?ScOxYWtpIMOaY2Fy?=