[R] Is there a sexy way ...?

Stephen Berman @tephen@berm@n @end|ng |rom gmx@net
Fri Sep 27 10:32:44 CEST 2024


Yet another way (not as sexy as Deepayan's):

as.vector(t(sapply(x, c)))

Steve Berman

On Fri, 27 Sep 2024 10:45:06 +0300 Eric Berger <ericjberger using gmail.com> wrote:

> v <- as.numeric(matrix(unlist(x),ncol=5,byrow=TRUE))
> v
>  [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3
>
> On Fri, Sep 27, 2024 at 8:33 AM Deepayan Sarkar
> <deepayan.sarkar using gmail.com> wrote:
>>
>> > unsplit(x, f)
>>  [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3
>>
>> Is more general (works if the subgroups are imbalanced), and hopefully more
>> sexy as well :-)
>>
>> Best,
>> -Deepayan
>>
>>
>> On Fri, 27 Sept 2024 at 10:11, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>>
>> > ... And, in fact, I just realized that
>> >
>> > c(do.call(rbind, x))
>> >
>> > is even better.
>> >
>> > -- Bert
>> >
>> >
>> > On Thu, Sep 26, 2024 at 9:26 PM Bert Gunter <bgunter.4567 using gmail.com>
>> > wrote:
>> >
>> > > Sorry, hit send by accident.
>> > > The 2-line version is:
>> > >
>> > > x <- do.call(rbind, x)
>> > > dim(x) <- NULL
>> > >
>> > > Cheers,
>> > > Bert
>> > >
>> > > On Thu, Sep 26, 2024 at 9:23 PM Bert Gunter <bgunter.4567 using gmail.com>
>> > > wrote:
>> > >
>> > >> How about:
>> > >> as.vector(do.call(rbind,x))
>> > >>
>> > >> Cheers,
>> > >> Bert
>> > >>
>> > >>
>> > >>
>> > >>
>> > >> However, I much prefer a 2 line version:
>> > >>
>> > >> On Thu, Sep 26, 2024 at 8:56 PM Rolf Turner <rolfturner using posteo.net>
>> > >> wrote:
>> > >>
>> > >>>
>> > >>> I have (toy example):
>> > >>>
>> > >>> x <- list(`1` = c(7, 13, 1, 4, 10),
>> > >>>           `2` = c(2, 5,  14, 8, 11),
>> > >>>           `3` = c(6, 9, 15, 12, 3))
>> > >>> and
>> > >>>
>> > >>> f <- factor(rep(1:3,5))
>> > >>>
>> > >>> I want to create a vector v of length 15 such that the entries of v,
>> > >>> corresponding to level l of f are the entries of x[[l]].  I.e. I want
>> > >>> v to equal
>> > >>>
>> > >>>     c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3)
>> > >>>
>> > >>> I can create v "easily enough", using say, a for-loop.  It seems to me,
>> > >>> though, that there should be sexier (single command) way of achieving
>> > >>> the desired result.  However I cannot devise one.
>> > >>>
>> > >>> Can anyone point me in the right direction?  Thanks.
>> > >>>
>> > >>> cheers,
>> > >>>
>> > >>> Rolf Turner
>> > >>>
>> > >>> --
>> > >>> Honorary Research Fellow
>> > >>> Department of Statistics
>> > >>> University of Auckland
>> > >>> Stats. Dep't. (secretaries) phone:
>> > >>>          +64-9-373-7599 ext. 89622
>> > >>> Home phone: +64-9-480-4619
>> > >>>
>> > >>> ______________________________________________
>> > >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > >>> https://stat.ethz.ch/mailman/listinfo/r-help
>> > >>> PLEASE do read the posting guide
>> > >>> https://www.R-project.org/posting-guide.html
>> > >>> and provide commented, minimal, self-contained, reproducible code.
>> > >>>
>> > >>
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > https://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list