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

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Sat Sep 28 19:57:51 CEST 2024


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 <polc1410 using gmail.com> 
Sent: Saturday, September 28, 2024 5:27 AM
To: avi.e.gross using gmail.com
Cc: Lennart Kasserra <lennart.kasserra using gmail.com>; Rolf Turner <rolfturner using posteo.net>; r-help using r-project.org
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 , <avi.e.gross using gmail.com <mailto:avi.e.gross using gmail.com> > 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 <r-help-bounces using r-project.org <mailto:r-help-bounces using r-project.org> > On Behalf Of Lennart Kasserra
Sent: Saturday, September 28, 2024 1:59 AM
To: Rolf Turner <rolfturner using posteo.net <mailto:rolfturner using posteo.net> >; r-help using r-project.org <mailto:r-help using r-project.org> ; lennart.kasserra using gmail.com <mailto:lennart.kasserra using gmail.com> 
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
>>

______________________________________________
R-help using r-project.org <mailto: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.

______________________________________________
R-help using r-project.org <mailto: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]]



More information about the R-help mailing list