[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 08:15:13 CEST 2024


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> On Behalf Of Lennart Kasserra
Sent: Saturday, September 28, 2024 1:59 AM
To: Rolf Turner <rolfturner using posteo.net>; r-help using r-project.org; 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 mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list