This post obviously beats a fossilized horse, so feel free to ignore.
So here's my problem with my "solution" to Rolf's query as well as
several others: it assumes that one knows the details of how matrices
are stored as vectors with a 'dim' attribute. Although this might be
considered elementary "common" knowledge, I note that it is not
documented in ?matrix , although it is in ?array ("The values in data
are taken to be those in the array with the leftmost subscript moving
fastest.") . So imo maybe not obvious to a "casual" user of R. Such a
user might therefore be flummoxed by my (and similar) "solutions" to
Rolf's query.
As Rolf and others noted, a simple loop-type approach using indexing
does the job nicely. Full stop. (And Deepayan's solution seems
"right" when you have the associated factor to provide the grouping).
But if one wanted to use only simple vector indexing -- no array
intermediary -- then the regular structure of Rolf's problem makes
this straightforward using a little arithmetic and sorting.
## breaking things up for clarity
> z <- unname(unlist(x)) ## unlist returns a named vector; so remove them for
> clarity
> x <- seq_along(z) %% -length(x[[1]])
> z[order(ix)]
[1] 7 2 6 13 5 9 1 14 15 4 8 12 10 11 3
I make no claim for either elegance or speed. Nor "sexiness". There
may also be cleverer aritmetic that one should use.
Cheers,
Bert
On Fri, Sep 27, 2024 at 1:39 AM Mark Leeds <[email protected]> wrote:
>
> Rolf can tell us for sure but I thought the goal was to use v ?
> Maybe not ? Either way, I think Bert wins for shortest and Kimmo
> wins for longest. IMHO, elegance is in the eye of the
> beholder.
>
>
>
>
>
>
> On Fri, Sep 27, 2024 at 4:35 AM Stephen Berman via R-help <
> [email protected]> wrote:
>
> > 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 <[email protected]>
> > 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
> > > <[email protected]> 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 <[email protected]>
> > 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 <[email protected]>
> > >> > 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 <[email protected]
> > >
> > >> > > 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 <[email protected]
> > >
> > >> > >> 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
> > >> > >>>
> > >> > >>> ______________________________________________
> > >> > >>> [email protected] 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]]
> > >> >
> > >> > ______________________________________________
> > >> > [email protected] 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]]
> > >>
> > >> ______________________________________________
> > >> [email protected] 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.
> >
> > ______________________________________________
> > [email protected] 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]]
>
> ______________________________________________
> [email protected] 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.
______________________________________________
[email protected] 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.