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

2024-09-30 Thread peter dalgaard
Yes, unsplit() it is. I was messing around with ave() (which can be hammered 
into submission, sort of).

My overlooking unsplit() is somewhat impressive in view of "svn diff -c 
18591"

-pd

> On 27 Sep 2024, at 17:08 , Martin Maechler  wrote:
> 
>> Chris Evans via R-help 
>>on Fri, 27 Sep 2024 12:20:47 +0200 writes:
> 
>> Oh glorious!  Thanks Duncan.
>> Fortune cookie nomination!
> 
> I don't  disagree with the nomination -- thank you, Duncan!
> 
> However, please note that I'm sure Rolf's was challenged /
> question was ment to work correctly for all  factors `f`  with
> levels "1", "2", "3".
> 
> Almost all solution were simply assuming that the toy example
> `f` was the real `f`, but that's not realistic.
> 
> Consequently, in my view, the only valid proposition and a very
> nice one, indeed, was  Deepayan's (well, he's "R core", ...) 
> 
>   unsplit(x, f)
> 
> Martin
> 
>> On 27/09/2024 11:13, Duncan Murdoch wrote:
>>> On 2024-09-26 11:55 p.m., Rolf Turner 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.
 
>>> 
>>> Don't you find a for loop's naked display of intention to be sexy?
>>> 
>>> Duncan Murdoch
>>> 
>> -- 
>> Chris Evans (he/him)
>> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
>> University of Roehampton, London, UK.
>> CORE site: http://www.coresystemtrust.org.uk
>> Other work web site: https://www.psyctc.org/psyctc/
>> Personal site: https://www.psyctc.org/pelerinage2016/
> 
>> __
>> [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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [email protected]  Priv: [email protected]

__
[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.


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

2024-09-28 Thread Richard O'Keefe
I for one am grateful to have been reminded of the existence of split
and especially unsplit.

On Sun, 29 Sept 2024 at 15:48,  wrote:
>
> Admit it, Rolf. Haven't you wondered if S, in a more private way, is sexier 
> than R?
>
> OK, kidding aside, we have talked this to death.
>
> Just FYI, the conversation was stimulating for some of us and I have 
> continued on my own and located functions I see as useful in the stringi and 
> stringr packages to make my silly version ever less silly! LOL!
>
>
> -Original Message-
> From: Rolf Turner 
> Sent: Saturday, September 28, 2024 10:11 PM
> To: CALUM POLWART 
> Cc: [email protected]; Lennart Kasserra ; 
> [email protected]
> Subject: Re: [R] Is there a sexy way ...?
>
>
>
> On Sat, 28 Sep 2024 10:26:31 +0100
> CALUM POLWART  wrote:
>
> > Avi
> >
> > I fear this was all a huge social experiment.
> >
> > Testing if a post titled "sexy way" would increase engagement...
>
> 
>
> I conjecture that this conjecture was tongue-in-cheek.  Be that as it
> were 😊️, let me assure everyone that such was not my intention.  The
> usage is just my manner of speaking.
>
> cheers,
>
> Rolf
>
> --
> 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.

__
[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.


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

2024-09-28 Thread avi.e.gross
Admit it, Rolf. Haven't you wondered if S, in a more private way, is sexier 
than R?

OK, kidding aside, we have talked this to death.

Just FYI, the conversation was stimulating for some of us and I have continued 
on my own and located functions I see as useful in the stringi and stringr 
packages to make my silly version ever less silly! LOL!


-Original Message-
From: Rolf Turner  
Sent: Saturday, September 28, 2024 10:11 PM
To: CALUM POLWART 
Cc: [email protected]; Lennart Kasserra ; 
[email protected]
Subject: Re: [R] Is there a sexy way ...?



On Sat, 28 Sep 2024 10:26:31 +0100
CALUM POLWART  wrote:

> Avi
> 
> I fear this was all a huge social experiment.
> 
> Testing if a post titled "sexy way" would increase engagement...



I conjecture that this conjecture was tongue-in-cheek.  Be that as it
were 😊️, let me assure everyone that such was not my intention.  The
usage is just my manner of speaking.

cheers,

Rolf

-- 
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.


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

2024-09-28 Thread Rolf Turner



On Sat, 28 Sep 2024 10:26:31 +0100
CALUM POLWART  wrote:

> Avi
> 
> I fear this was all a huge social experiment.
> 
> Testing if a post titled "sexy way" would increase engagement...



I conjecture that this conjecture was tongue-in-cheek.  Be that as it
were 😊️, let me assure everyone that such was not my intention.  The
usage is just my manner of speaking.

cheers,

Rolf

-- 
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.


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

2024-09-28 Thread avi.e.gross
John,

I thought some more about the topic overnight. Of course, "sexy" is not a
great analogy.

But consider a concept of how to do something cleverly or creatively or in
ways others might not easily come up with as the standard way(s) are
commonly used.

I threw something together in middle of the night that violated some rules
of sorts but maybe not. I mean the standard ways of dealing with what looked
like integers or at least of type numeric, is to leave them as integers. My
earlier attempt did that as it converted a list of vectors into something
else like a data.frame or matrix.

My later attempt jumped out of the box. This time I focused on the concept
of the python zip function that takes multiple iterables and weaves them
into an iterable of n-tuples. 

My first though was that among many other things, paste/paste0 does that.
There is also a family of functions in the map family and one called pmap
that takes multiple vectors and applies a function to them. An example
calculates the rowwise sum, of sorts, not that we need this way:

> pmap(.l = list(1:4, 2:5, 3:6), .f=sum)
[[1]]
[1] 6

[[2]]
[1] 9

[[3]]
[1] 12

[[4]]
[1] 15

If what you want is to refer to the arguments directly, as someone pointed
out, you can use an anonymous function like so to produce a 3-tuple:

> pmap(.l = list(1:4, 2:5, 3:6), .f=\(a, b, c) c(a, b, c))
[[1]]
[1] 1 2 3

[[2]]
[1] 2 3 4

[[3]]
[1] 3 4 5

[[4]]
[1] 4 5 6


Or use a formula with positional notation that gets the same output:

pmap(.l = list(1:4, 2:5, 3:6), ~ c(..1, ..2, ..3))

I have mostly used pmap directly on dataframes but it works fine on any list
of vectors. So, since the x we have been using is a list of vectors, this
works:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3))

pmap(.l = x, ~ c(..1, ..2, ..3))

The result though is not flat, so unlist it:

> pmap(.l = x, ~ c(..1, ..2, ..3)) |> unlist()
 [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

I would consider this a relatively direct and simple answer, except it does
not deal with the factor issue that was later explained. A problem is that
the above has three items hard-coded and needs changes to work with an
arbitrary number of columns of data. It can be done, just less elegantly. 

But I was not thinking about pmap at night, I was thinking about paste and
unfortunately, paste is really more about TEXT. Everything is converted to
text so the operations I would need to do would be manipulating text.

I later realized my earlier work was too elaborate. Paste allows you to both
make comma separated parts and then combine them into one big string with a
separator. In this case, comma.

So here is the new and hopefully shorter and more efficient version instead
of two paste statements in a row:

> paste(x$`1`, x$`2`, x$`3`, sep=",", collapse=",")
[1] "7,2,6,13,5,9,1,14,15,4,8,12,10,11,3"

Of course, to accommodate any number of vectors to combine, and without
needing to know their names or specify positions, the do.call concept that
expands the contents into individual arguments, is great:

do.call(paste, c(x, sep=",", collapse=","))

All that is needed is to deal taking a string with lots of commas and
resurrecting it:

do.call(paste, c(x, sep=",", collapse=",")) |>
  strsplit(",") |>
  unlist() |>
  as.integer()

Or without the new R pipe:

as.integer(unlist(strsplit(do.call(paste, 
   c(x, 
 sep=",", 
 collapse=",")),
   ",")))

The above looks better in a constant width font, LOL!

I will say it is annoying why a version of strsplit is not easily available
that does something more trivial. Given a single string and a fixed
separator such as a space or comma, meaning no regular expression, break it
up into a vector containing the parts. No need to unlist. Just take the
result and make it integer or whatever you need. It looks like a fairly
trivial function to make.

I will add one last idea and perhaps let this thread wane. Some things are a
bit like religion to people or a matter of taste. There are arguments
ranging in another forum on why a language does not have some form of loop
like DO ... UNTIL because some people HATE using break statements even when
they make perfect sense. Others are happier with a while(True) construct
that makes clear the contents will decide when to break out. To expect
people to agree on what is "sexy" is not a reasonable expectation. LOL!


-Original Message-----
From: Sorkin, John  
Sent: Saturday, September 28, 2024 12:01 AM
To: [email protected]; 'Rolf Turner' ;
[email protected]
Subject: Re: [R] Is there a sexy way ...?

"Sexy code" may get a job done and demonstrate the code's knowl

Re: [R] Is there a sexy way ...? Fortune nomination

2024-09-28 Thread Achim Zeileis via R-help

On Sat, 28 Sep 2024, J C Nash wrote:


On 2024-09-28 13:57, [email protected] wrote:

Python users often ask if a solution is “pythonic”. But I am not aware
of R users having any special name like “R-thritic” and that may be a
good thing.


Nice, added on R-Forge :-)
Achim


__
[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.


Re: [R] Is there a sexy way ...? Fortune nomination

2024-09-28 Thread J C Nash




On 2024-09-28 13:57, [email protected] wrote:

Python users often ask if a solution is “pythonic”. But I am not aware
of R users having any special name like “R-thritic” and that may be a
good thing.


__
[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.


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

2024-09-28 Thread avi.e.gross
Calum,

 

I know Rolf for a while so I will not accept any calumny about his intentions. 
He stated what he wanted, albeit imperfectly, and interacted with us as we came 
up with ideas.

 

I have seen others who ask some open-ended question, often using a brand new 
idea, and do not interact. Some rumors have it that there may be motives 
ranging from wasting everyone’s time to gathering the best ideas into a book or 
something.

 

It is amusing that Rolf chose “sexy” so what would be acceptable?

 

I think it is often fair to ask for a more efficient way, or to ask if there is 
a package that does it or makes it easier or to ask how to do it in the base 
language or how to make it handle errors and so on.

 

In some languages, people talk about whether some code conforms to the goals 
and ideas of a language. Python users often ask if a solution is “pythonic”. 
But I am not aware of R users having any special name like “R-thritic” and that 
may be a good thing.

 

 

From: CALUM POLWART  
Sent: Saturday, September 28, 2024 5:27 AM
To: [email protected]
Cc: Lennart Kasserra ; Rolf Turner 
; [email protected]
Subject: Re: [R] Is there a sexy way ...?

 

Avi

I fear this was all a huge social experiment.

Testing if a post titled "sexy way" would increase engagement...

 

On Sat, 28 Sep 2024, 07:21 , mailto:[email protected]> > wrote:

I see a book coming:
"666 ways to do the same thing in R ranked by sexiness."

Kidding aside, if you look under the covers of some of the functions we are 
using, we may find we are taking steps back as some of them use others and 
perhaps more functionality than we need.

But for a new reader , looking at many approaches may open up other ways and 
ideas and see the problem space as quite vast.

-Original Message-
From: R-help mailto:[email protected]> > On Behalf Of Lennart Kasserra
Sent: Saturday, September 28, 2024 1:59 AM
To: Rolf Turner mailto:[email protected]> >; 
[email protected] <mailto:[email protected]> ; [email protected] 
<mailto:[email protected]> 
Subject: Re: [R] Is there a sexy way ...?

Sorry to append, but I just realised that of course

```

x |>
   pmap(c) |>
   reduce(c) |>
   unname()

```

also works and is a general solution in case your list has more than 
three elements. Here, we map in parallel over all elements of the list, 
always combining the current set of elements into a vector, and then 
reduce the resulting list into a vector by combining the elements in 
order. This yields a named vector which we can un-name given this was 
not desired.n

All the best,

Lennart

Am 28.09.24 um 07:52 schrieb Lennart Kasserra:
> Hi Rolf,
>
> this topic is probably already saturated, but here is a tidyverse 
> solution:
>
> ```
>
> library(purrr)
>
> x <- list(
>   `1` = c(7, 13, 1, 4, 10),
>   `2` = c(2, 5,  14, 8, 11),
>   `3` = c(6, 9, 15, 12, 3)
> )
>
> x |>
>   pmap(~ c(..1, ..2, ..3)) |>
>   reduce(c)
>
> #> [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3
>
> ```
>
> Here, we map over the elements of the list in parallel (hence pmap), 
> always combining the elements at the current position into a vector, 
> which will result in a list like this:
>
> ```
>
> [[1]]
> [1] 7 2 6
>
> [[2]]
> [1] 13  5  9
>
> ...
>
> ```
>
> And then we reduce this resulting list into a vector by successively 
> combining its elements with `c()`. I think the formula syntax is a bit 
> idiosyncratic, you could also do this with an anonymous function like 
> pmap(\(`1`, `2`, `3`) c(`1`, `2`, `3`)), or if the list was unnamed as 
> pmap(\(x, y, z) c(x, y, z)).
>
> I personally find the tidyverse-esque code to be very explicit & 
> readable, but given base R can do this very concisely one might argue 
> that it is superfluous to bring in an extra library for this. I think 
> Bert's solution (
> `c(do.call(rbind, x))`) is great if `f` has no substantive meaning, 
> and Deepayan's solution (`unsplit(x, f)`) is perfect in case it does - 
> does not get much sexier than that, I am afraid.
>
> Best,
>
> Lennart
>
>
> Am 27.09.24 um 05:55 schrieb Rolf Turner:
>> 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 m

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

2024-09-28 Thread CALUM POLWART
Avi

I fear this was all a huge social experiment.

Testing if a post titled "sexy way" would increase engagement...

On Sat, 28 Sep 2024, 07:21 ,  wrote:

> I see a book coming:
> "666 ways to do the same thing in R ranked by sexiness."
>
> Kidding aside, if you look under the covers of some of the functions we
> are using, we may find we are taking steps back as some of them use others
> and perhaps more functionality than we need.
>
> But for a new reader , looking at many approaches may open up other ways
> and ideas and see the problem space as quite vast.
>
> -Original Message-
> From: R-help  On Behalf Of Lennart Kasserra
> Sent: Saturday, September 28, 2024 1:59 AM
> To: Rolf Turner ; [email protected];
> [email protected]
> Subject: Re: [R] Is there a sexy way ...?
>
> Sorry to append, but I just realised that of course
>
> ```
>
> x |>
>pmap(c) |>
>reduce(c) |>
>unname()
>
> ```
>
> also works and is a general solution in case your list has more than
> three elements. Here, we map in parallel over all elements of the list,
> always combining the current set of elements into a vector, and then
> reduce the resulting list into a vector by combining the elements in
> order. This yields a named vector which we can un-name given this was
> not desired.n
>
> All the best,
>
> Lennart
>
> Am 28.09.24 um 07:52 schrieb Lennart Kasserra:
> > Hi Rolf,
> >
> > this topic is probably already saturated, but here is a tidyverse
> > solution:
> >
> > ```
> >
> > library(purrr)
> >
> > x <- list(
> >   `1` = c(7, 13, 1, 4, 10),
> >   `2` = c(2, 5,  14, 8, 11),
> >   `3` = c(6, 9, 15, 12, 3)
> > )
> >
> > x |>
> >   pmap(~ c(..1, ..2, ..3)) |>
> >   reduce(c)
> >
> > #> [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3
> >
> > ```
> >
> > Here, we map over the elements of the list in parallel (hence pmap),
> > always combining the elements at the current position into a vector,
> > which will result in a list like this:
> >
> > ```
> >
> > [[1]]
> > [1] 7 2 6
> >
> > [[2]]
> > [1] 13  5  9
> >
> > ...
> >
> > ```
> >
> > And then we reduce this resulting list into a vector by successively
> > combining its elements with `c()`. I think the formula syntax is a bit
> > idiosyncratic, you could also do this with an anonymous function like
> > pmap(\(`1`, `2`, `3`) c(`1`, `2`, `3`)), or if the list was unnamed as
> > pmap(\(x, y, z) c(x, y, z)).
> >
> > I personally find the tidyverse-esque code to be very explicit &
> > readable, but given base R can do this very concisely one might argue
> > that it is superfluous to bring in an extra library for this. I think
> > Bert's solution (
> > `c(do.call(rbind, x))`) is great if `f` has no substantive meaning,
> > and Deepayan's solution (`unsplit(x, f)`) is perfect in case it does -
> > does not get much sexier than that, I am afraid.
> >
> > Best,
> >
> > Lennart
> >
> >
> > Am 27.09.24 um 05:55 schrieb Rolf Turner:
> >> 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
> >>
>
> __
> [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.


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

2024-09-27 Thread avi.e.gross
I see a book coming:
"666 ways to do the same thing in R ranked by sexiness."

Kidding aside, if you look under the covers of some of the functions we are 
using, we may find we are taking steps back as some of them use others and 
perhaps more functionality than we need.

But for a new reader , looking at many approaches may open up other ways and 
ideas and see the problem space as quite vast.

-Original Message-
From: R-help  On Behalf Of Lennart Kasserra
Sent: Saturday, September 28, 2024 1:59 AM
To: Rolf Turner ; [email protected]; 
[email protected]
Subject: Re: [R] Is there a sexy way ...?

Sorry to append, but I just realised that of course

```

x |>
   pmap(c) |>
   reduce(c) |>
   unname()

```

also works and is a general solution in case your list has more than 
three elements. Here, we map in parallel over all elements of the list, 
always combining the current set of elements into a vector, and then 
reduce the resulting list into a vector by combining the elements in 
order. This yields a named vector which we can un-name given this was 
not desired.n

All the best,

Lennart

Am 28.09.24 um 07:52 schrieb Lennart Kasserra:
> Hi Rolf,
>
> this topic is probably already saturated, but here is a tidyverse 
> solution:
>
> ```
>
> library(purrr)
>
> x <- list(
>   `1` = c(7, 13, 1, 4, 10),
>   `2` = c(2, 5,  14, 8, 11),
>   `3` = c(6, 9, 15, 12, 3)
> )
>
> x |>
>   pmap(~ c(..1, ..2, ..3)) |>
>   reduce(c)
>
> #> [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3
>
> ```
>
> Here, we map over the elements of the list in parallel (hence pmap), 
> always combining the elements at the current position into a vector, 
> which will result in a list like this:
>
> ```
>
> [[1]]
> [1] 7 2 6
>
> [[2]]
> [1] 13  5  9
>
> ...
>
> ```
>
> And then we reduce this resulting list into a vector by successively 
> combining its elements with `c()`. I think the formula syntax is a bit 
> idiosyncratic, you could also do this with an anonymous function like 
> pmap(\(`1`, `2`, `3`) c(`1`, `2`, `3`)), or if the list was unnamed as 
> pmap(\(x, y, z) c(x, y, z)).
>
> I personally find the tidyverse-esque code to be very explicit & 
> readable, but given base R can do this very concisely one might argue 
> that it is superfluous to bring in an extra library for this. I think 
> Bert's solution (
> `c(do.call(rbind, x))`) is great if `f` has no substantive meaning, 
> and Deepayan's solution (`unsplit(x, f)`) is perfect in case it does - 
> does not get much sexier than that, I am afraid.
>
> Best,
>
> Lennart
>
>
> Am 27.09.24 um 05:55 schrieb Rolf Turner:
>> 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
>>

__
[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.


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

2024-09-27 Thread Lennart Kasserra

Sorry to append, but I just realised that of course

```

x |>
  pmap(c) |>
  reduce(c) |>
  unname()

```

also works and is a general solution in case your list has more than 
three elements. Here, we map in parallel over all elements of the list, 
always combining the current set of elements into a vector, and then 
reduce the resulting list into a vector by combining the elements in 
order. This yields a named vector which we can un-name given this was 
not desired.n


All the best,

Lennart

Am 28.09.24 um 07:52 schrieb Lennart Kasserra:

Hi Rolf,

this topic is probably already saturated, but here is a tidyverse 
solution:


```

library(purrr)

x <- list(
  `1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3)
)

x |>
  pmap(~ c(..1, ..2, ..3)) |>
  reduce(c)

#> [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

```

Here, we map over the elements of the list in parallel (hence pmap), 
always combining the elements at the current position into a vector, 
which will result in a list like this:


```

[[1]]
[1] 7 2 6

[[2]]
[1] 13  5  9

...

```

And then we reduce this resulting list into a vector by successively 
combining its elements with `c()`. I think the formula syntax is a bit 
idiosyncratic, you could also do this with an anonymous function like 
pmap(\(`1`, `2`, `3`) c(`1`, `2`, `3`)), or if the list was unnamed as 
pmap(\(x, y, z) c(x, y, z)).


I personally find the tidyverse-esque code to be very explicit & 
readable, but given base R can do this very concisely one might argue 
that it is superfluous to bring in an extra library for this. I think 
Bert's solution (
`c(do.call(rbind, x))`) is great if `f` has no substantive meaning, 
and Deepayan's solution (`unsplit(x, f)`) is perfect in case it does - 
does not get much sexier than that, I am afraid.


Best,

Lennart


Am 27.09.24 um 05:55 schrieb Rolf Turner:

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



__
[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.


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

2024-09-27 Thread Lennart Kasserra

Hi Rolf,

this topic is probably already saturated, but here is a tidyverse solution:

```

library(purrr)

x <- list(
  `1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3)
)

x |>
  pmap(~ c(..1, ..2, ..3)) |>
  reduce(c)

#> [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

```

Here, we map over the elements of the list in parallel (hence pmap), 
always combining the elements at the current position into a vector, 
which will result in a list like this:


```

[[1]]
[1] 7 2 6

[[2]]
[1] 13  5  9

...

```

And then we reduce this resulting list into a vector by successively 
combining its elements with `c()`. I think the formula syntax is a bit 
idiosyncratic, you could also do this with an anonymous function like 
pmap(\(`1`, `2`, `3`) c(`1`, `2`, `3`)), or if the list was unnamed as 
pmap(\(x, y, z) c(x, y, z)).


I personally find the tidyverse-esque code to be very explicit & 
readable, but given base R can do this very concisely one might argue 
that it is superfluous to bring in an extra library for this. I think 
Bert's solution (
`c(do.call(rbind, x))`) is great if `f` has no substantive meaning, and 
Deepayan's solution (`unsplit(x, f)`) is perfect in case it does - does 
not get much sexier than that, I am afraid.


Best,

Lennart


Am 27.09.24 um 05:55 schrieb Rolf Turner:

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



__
[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.


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

2024-09-27 Thread avi.e.gross
You are, of course, correct, John. But in a strange way, many people end up
finding their wife or husband not so sexy after many years and find others
now seem to be.

R is not about sex and it was not the ideal choice of words.

I think what was wanted was something brief rather than taking many lines as
my second somewhat joking solution was. Even that, was written off the top
of my head and I now have thought a bit about ways to really simplify it.
For example, forget my commas and use spaces. Break the one long sentence up
without using regular expressions and showing a result already as a vector. 

And, frankly, often the best code involves writing one or more fairly small
functions with each being easy to understand. Add a few comments. And,
although it may not be the most efficient, it may LOOK somewhat simple and
direct.

Rolf did explain a bit more and clearly many of us solved a different
problem. But I note that using named lists may not have been the best choice
for working on his problem and choosing another form of data that allows
transposition of dimensions might have been a simpler task if that is what
he wanted.

Rolf suggested he did not already know some of the gimmicks used and he has
a point. He may want to learn more because of code like this and somewhat
similar but different for data.frames. This may not be useful for this app
with a requirement about factors but may motivate using other data formats
when useful.

> numbers <- 1:24
> print(numbers)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> mat <- matrix(numbers, nrow=6, byrow=TRUE)
> print(mat)
 [,1] [,2] [,3] [,4]
[1,]1234
[2,]5678
[3,]9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20
[6,]   21   22   23   24
> print(as.vector(mat))
 [1]  1  5  9 13 17 21  2  6 10 14 18 22  3  7 11 15 19 23  4  8 12 16 20 24
> mat <- matrix(numbers, nrow=6, byrow=FALSE)
> print(mat)
 [,1] [,2] [,3] [,4]
[1,]17   13   19
[2,]28   14   20
[3,]39   15   21
[4,]4   10   16   22
[5,]5   11   17   23
[6,]6   12   18   24
> print(as.vector(mat))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> mat <- matrix(numbers, ncol=6, byrow=TRUE)
> print(mat)
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]123456
[2,]789   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
> print(as.vector(mat))
 [1]  1  7 13 19  2  8 14 20  3  9 15 21  4 10 16 22  5 11 17 23  6 12 18 24
> mat <- matrix(numbers, ncol=6, byrow=FALSE)
> print(mat)
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]159   13   17   21
[2,]26   10   14   18   22
[3,]37   11   15   19   23
[4,]48   12   16   20   24
> print(as.vector(mat))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> t(mat)
 [,1] [,2] [,3] [,4]
[1,]1234
[2,]5678
[3,]9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20
[6,]   21   22   23   24
> mat <- matrix(numbers, nrow=6, byrow=TRUE)
> mat2 <- matrix(mat, nrow=4)
> print(mat)
 [,1] [,2] [,3] [,4]
[1,]1234
[2,]5678
[3,]9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20
[6,]   21   22   23   24
> print(mat2)
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]1   17   103   19   12
[2,]5   21   147   23   16
[3,]92   18   114   20
[4,]   136   22   158   24
> df <- data.frame(a=1:6, b=7:12, c=13:18, d=19:24)
> print(df)
  a  b  c  d
1 1  7 13 19
2 2  8 14 20
3 3  9 15 21
4 4 10 16 22
5 5 11 17 23
6 6 12 18 24
> print(t(df))
  [,1] [,2] [,3] [,4] [,5] [,6]
a123456
b789   10   11   12
c   13   14   15   16   17   18
d   19   20   21   22   23   24

-Original Message-
From: Sorkin, John  
Sent: Saturday, September 28, 2024 12:01 AM
To: [email protected]; 'Rolf Turner' ;
[email protected]
Subject: Re: [R] Is there a sexy way ...?

"Sexy code" may get a job done and demonstrate the code's knowledge of a
programming language, but it often does this at the expense of clear, easy
to document (i.e. annotate what the code does), easy to read, and easy to
understand code. I fear that this is what this thread has developed "sexy"
but not easily understandable code. While I send kudos to all of you,
remember that sometimes simpler, while not as sexy can be better in the long
run. ;)

John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of
Medicine Claude D. Pepper Older Ame

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

2024-09-27 Thread Sorkin, John
"Sexy code" may get a job done and demonstrate the code's knowledge of a 
programming language, but it often does this at the expense of clear, easy to 
document (i.e. annotate what the code does), easy to read, and easy to 
understand code. I fear that this is what this thread has developed "sexy" but 
not easily understandable code. While I send kudos to all of you, remember that 
sometimes simpler, while not as sexy can be better in the long run. ;)

John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;
Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382





From: R-help  on behalf of [email protected] 

Sent: Friday, September 27, 2024 10:48 PM
To: 'Rolf Turner'; [email protected]
Subject: Re: [R] Is there a sexy way ...?

Rold,

We need to be clear on what makes an answer sexy! LOL!

I decided it was sexy to do it in a way that nobody (normal) would and had
not suggested yet.

Here is an original version I will explain in a minute. Or, maybe best a bit
before. Hee is the unformatted result whicvh is a tad hard to read but will
be made readable soon:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3))

as.integer(unlist(strsplit(as.vector(paste(paste(x$`1`, x$`2`, x$`3`,
sep=","), collapse=",")), split=",")))

The result is: 7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

After reading what others wrote, the following is more general one where any
number of vectors in a list can be handled:

as.integer(unlist(strsplit(as.vector(paste(do.call(paste, c(x, sep=",")),
collapse=",")), split=",")))

Perhaps a tad more readable is a version using the new pipe but for obvious
reasons, the dplyr/magrittr pipe works better for me than having to create
silly anonymous functions instead of using a period. You now have a
pipeline:

library(dplyr)

x %>%
  c(sep=",") %>%
  do.call(paste, .) %>%
  paste(collapse=",") %>%
  as.vector() %>%
  strsplit(split=",") %>%
  unlist() %>%
  as.integer()

And it returns the right answer!

- You start with x and pipe it as

- the first argument to c() and the second argument already in place is an
option to later use comma as a separator

- that is piped to a do.call() which takes that c() tuple and replaces the
second argument of period with it. You now have taken the original data and
made three text strings like so:
"7,2,6"   "13,5,9"  "1,14,15" "4,8,12"  "10,11,3"

- But you want all those strings collapsed into a single long string with
commas between the parts. Do another paste this time putting the substrings
together and collapsing with a comma. The results is:
"7,2,6,13,5,9,1,14,15,4,8,12,10,11,3"

- But that is not a vector and don't ask why!

- Now split that string at commas:
"7"  "2"  "6"  "13" "5"  "9"  "1"  "14" "15" "4"  "8"  "12" "10" "11" "3"

- and undo the odd list format it returns to flatten it back into a
character vector:
"7"  "2"  "6"  "13" "5"  "9"  "1"  "14" "15" "4"  "8"  "12" "10" "11" "3"

- Yep it looks the same but is subtly different. Time to make it into
integers or whatever:
7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

Looked at after the fact, it seems so bloody obvious! And the chance of
someone else trying this approach, justifiably, is low, LOL!

One nice feature of the do.call is this can be extended like so:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3),
  `4` = c( 101, 102, 103, 104, 105),
  `5` = c(-105, -104, -103, -102, -101))

Works fine and does this for the now five columns:

[1]726  101 -105   1359  102 -1041   14   15  103
-10348   12  104 -102
[21]   10   113  105 -101

My apologies to all who expected a more serious post. I have been focusing
on Python lately and over there, some things are done differently albeit I
probably would be using the numpy and pandas packages to do this or even a
simple list comprehension using zip:

# Python, not R.
 [

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

2024-09-27 Thread avi.e.gross
Rold,

We need to be clear on what makes an answer sexy! LOL!

I decided it was sexy to do it in a way that nobody (normal) would and had
not suggested yet.

Here is an original version I will explain in a minute. Or, maybe best a bit
before. Hee is the unformatted result whicvh is a tad hard to read but will
be made readable soon:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3))

as.integer(unlist(strsplit(as.vector(paste(paste(x$`1`, x$`2`, x$`3`,
sep=","), collapse=",")), split=",")))

The result is: 7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

After reading what others wrote, the following is more general one where any
number of vectors in a list can be handled:

as.integer(unlist(strsplit(as.vector(paste(do.call(paste, c(x, sep=",")),
collapse=",")), split=",")))

Perhaps a tad more readable is a version using the new pipe but for obvious
reasons, the dplyr/magrittr pipe works better for me than having to create
silly anonymous functions instead of using a period. You now have a
pipeline:

library(dplyr)

x %>%
  c(sep=",") %>%
  do.call(paste, .) %>%
  paste(collapse=",") %>%
  as.vector() %>%
  strsplit(split=",") %>%
  unlist() %>%
  as.integer()

And it returns the right answer!

- You start with x and pipe it as 

- the first argument to c() and the second argument already in place is an
option to later use comma as a separator

- that is piped to a do.call() which takes that c() tuple and replaces the
second argument of period with it. You now have taken the original data and
made three text strings like so:
"7,2,6"   "13,5,9"  "1,14,15" "4,8,12"  "10,11,3"

- But you want all those strings collapsed into a single long string with
commas between the parts. Do another paste this time putting the substrings
together and collapsing with a comma. The results is:
"7,2,6,13,5,9,1,14,15,4,8,12,10,11,3"

- But that is not a vector and don't ask why!

- Now split that string at commas:
"7"  "2"  "6"  "13" "5"  "9"  "1"  "14" "15" "4"  "8"  "12" "10" "11" "3"

- and undo the odd list format it returns to flatten it back into a
character vector:
"7"  "2"  "6"  "13" "5"  "9"  "1"  "14" "15" "4"  "8"  "12" "10" "11" "3"

- Yep it looks the same but is subtly different. Time to make it into
integers or whatever:
7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

Looked at after the fact, it seems so bloody obvious! And the chance of
someone else trying this approach, justifiably, is low, LOL!

One nice feature of the do.call is this can be extended like so:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3),
  `4` = c( 101, 102, 103, 104, 105),
  `5` = c(-105, -104, -103, -102, -101))

Works fine and does this for the now five columns:

[1]726  101 -105   1359  102 -1041   14   15  103
-10348   12  104 -102
[21]   10   113  105 -101

My apologies to all who expected a more serious post. I have been focusing
on Python lately and over there, some things are done differently albeit I
probably would be using the numpy and pandas packages to do this or even a
simple list comprehension using zip:

# Python, not R.
 [ (first, second, third) for first, second, third in zip(*x)]

[(7, 2, 6), (13, 5, 9), (1, 14, 15), (4, 8, 12), (10, 11, 3)]

And, of course, that needs to be made into a list of individual items 

# Python, not R.
[num 
 for elem in [(first, second, third) for first, second, third in zip(*x)] 
 for num in elem]

[7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3]

For any interested, you can combine python and R in the same program back
and forth on the same data inside what is still called RSTUDIO and if there
are times one allows a better or at least easier for you, way to do a
transformation, you can often mix and match.

-Original Message-
From: R-help  On Behalf Of Rolf Turner
Sent: Thursday, September 26, 2024 11:56 PM
To: [email protected]
Subject: [R] Is there a sexy way ...?


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/listi

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

2024-09-27 Thread Bert Gunter
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  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 
> > 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
> > >  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 
> > 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 
> > >> > 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  > >
> > >> > > 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  > >
> > >> > >> 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.
> > >> > >>>
> > >> > >>
> > >> >
> > >> >  

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

2024-09-27 Thread avi.e.gross
Rolf,

I, and many others have come up with an assortment of solutions that seem to
work, often by ignoring whatever you intend by mentioning f as a factor.

But consider a dumb question. Why are you starting with a list of vectors,
with odd pseudo-numeric names?

Many of the solutions started by converting your data structure into either
a data.frame of some kind or a matrix since all your data seemed numeric. 

Realistically, your item is pretty much already a data.frame minus the class
designation and a list of row names.

Once you have a 2-D representation, various methods allow you to rotate it,
or read it off a row at a time. 

But a deeper issue is looking at how you might approach this in other
languages such as Python that has a zip functionality. It is very common
there to want to iterate over multiple "lists" simultaneously, sort of by
giving it multiple columns and having it weaved together. An enumerate
version just adds a column of sequence numbers. 

Your example might look like this IN PYTHON:

 This is not meant for anything but illustration as something somebody
probably has already done in R if you can find some package that supports
this, but note the work "zip" is used for compression. And note Python uses
a concept of generators so that "zipped" below is a valid generator but to
be show must be coerced to run to completion as in asking for a list of it.
And it gets consumed after first use so you need to save it as a list
version for this exercise.

 PYTHON CODE 
x = [ (7, 13, 1, 4, 10),
  (2, 5,  14, 8, 11),
  (6, 9, 15, 12, 3)
]
zipped = list(zip(x[0], x[1], x[2]))

flattened = [num for elem in zipped for num in elem]

x
zipped
flattened

 OUTPUT ---
>>> x = [ (7, 13, 1, 4, 10),
...   (2, 5,  14, 8, 11),
...   (6, 9, 15, 12, 3)
... ]
>>> zipped = zip(x[0], x[1], x[2])
>>> flattened = [num for elem in zipped for num in elem]
>>> x
[(7, 13, 1, 4, 10), (2, 5, 14, 8, 11), (6, 9, 15, 12, 3)]
>>> zipped

>>> flattened
[7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3]
---END OUTPUT ---

I am not saying to use python, just showing other ways people find
comfortable. But choosing the right data structure can make things easy, or
at least easier.


-Original Message-
From: R-help  On Behalf Of Rolf Turner
Sent: Thursday, September 26, 2024 11:56 PM
To: [email protected]
Subject: [R] Is there a sexy way ...?


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.

__
[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.


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

2024-09-27 Thread Martin Maechler
> Chris Evans via R-help 
> on Fri, 27 Sep 2024 12:20:47 +0200 writes:

> Oh glorious!  Thanks Duncan.
> Fortune cookie nomination!

I don't  disagree with the nomination -- thank you, Duncan!

However, please note that I'm sure Rolf's was challenged /
question was ment to work correctly for all  factors `f`  with
levels "1", "2", "3".

Almost all solution were simply assuming that the toy example
`f` was the real `f`, but that's not realistic.

Consequently, in my view, the only valid proposition and a very
nice one, indeed, was  Deepayan's (well, he's "R core", ...) 

   unsplit(x, f)

Martin

> On 27/09/2024 11:13, Duncan Murdoch wrote:
>> On 2024-09-26 11:55 p.m., Rolf Turner 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.
>>> 
>> 
>> Don't you find a for loop's naked display of intention to be sexy?
>> 
>> Duncan Murdoch
>> 
> -- 
> Chris Evans (he/him)
> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
> University of Roehampton, London, UK.
> CORE site: http://www.coresystemtrust.org.uk
> Other work web site: https://www.psyctc.org/psyctc/
> Personal site: https://www.psyctc.org/pelerinage2016/

> __
> [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.


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

2024-09-27 Thread Chris Evans via R-help

Oh glorious!  Thanks Duncan.

Fortune cookie nomination!

On 27/09/2024 11:13, Duncan Murdoch wrote:

On 2024-09-26 11:55 p.m., Rolf Turner 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.



Don't you find a for loop's naked display of intention to be sexy?

Duncan Murdoch


--
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.

CORE site: http://www.coresystemtrust.org.uk
Other work web site: https://www.psyctc.org/psyctc/
Personal site: https://www.psyctc.org/pelerinage2016/

__
[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.


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

2024-09-27 Thread Duncan Murdoch

On 2024-09-26 11:55 p.m., Rolf Turner 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.



Don't you find a for loop's naked display of intention to be sexy?

Duncan Murdoch

__
[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.


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

2024-09-27 Thread Mark Leeds
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 
> 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
> >  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 
> 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 
> >> > 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  >
> >> > > 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  >
> >> > >> 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.


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

2024-09-27 Thread Stephen Berman via R-help
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  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
>  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  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 
>> > 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 
>> > > 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 
>> > >> 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.


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

2024-09-27 Thread Kimmo Elo
Dear Rolf, dear all,

this was an inspiring challenge :-) This seems to do the task...

--- snip ---

x <- list(`1` = c(7, 13, 1, 4, 10),
   `2` = c(2, 5,  14, 8, 11),
   `3` = c(6, 9, 15, 12, 3))

f <- factor(rep(1:3,5))

v <- as.vector(unlist(x)[ paste(rep(levels(f), length(x[[1]])), 
rep(1:length(x[[1]]), each=length(levels(f))), sep="") ])

v

--- snip ---

I leave it to you, whether this is an elegant solution or not ;-)

Cheers,

Kimmo

Rolf Turner kirjoitti 27.9.2024 klo 6.55:
> 
> 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
> 

-- 
Kimmo Elo
Senior Lecturer | Adjunct professor, Dr.

University of Eastern Finland
Department of Geographical and Historical Studies
P.O. Box 111
FIN-80101 Joensuu
Finland
E-mail: [email protected]
ResearchGate: www.researchgate.net/profile/Kimmo_Elo
LAWPOL Consortium (PI): https://lawpol.fi/en

__
[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.


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

2024-09-27 Thread Eric Berger
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
 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  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 
> > 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 
> > > 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 
> > >> 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.


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

2024-09-26 Thread Deepayan Sarkar
> 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  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 
> 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 
> > 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 
> >> 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.


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

2024-09-26 Thread Bert Gunter
... 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  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 
> 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 
>> 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.


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

2024-09-26 Thread Bert Gunter
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  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  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.


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

2024-09-26 Thread Bert Gunter
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  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.


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

2024-09-26 Thread avi.e.gross
Rolf,

This works, albeit you may not be thrilled:

x <- list(`1` = c(7, 13, 1, 4, 10),
  `2` = c(2, 5,  14, 8, 11),
  `3` = c(6, 9, 15, 12, 3)) 

as.vector(rbind(x[[1]], x[[2]], x[[3]]))

-- output:

> as.vector(rbind(x[[1]], x[[2]], x[[3]]))
 [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

-Original Message-
From: R-help  On Behalf Of Rolf Turner
Sent: Thursday, September 26, 2024 11:56 PM
To: [email protected]
Subject: [R] Is there a sexy way ...?


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.

__
[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.