[R] the opposite of pluck() in purrr

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Fri Nov 19 01:18:49 CET 2021


As noted, this is not the place to ask about dplyr but the answer you may
want is perhaps straight R.

If you have a list called weekdays and you know you o not want to take the
fifth, then indexing with -5 removes it:

> weekdays <- list("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
> weekdays[-5]
[[1]]
[1] "Sun"

[[2]]
[1] "Mon"

[[3]]
[1] "Tue"

[[4]]
[1] "Wed"

[[5]]
[1] "Fri"

[[6]]
[1] "Sat"

In general, any function you use to get one or more indices can be used sort
of like this:

> odder <- seq(from=1, to=length(weekdays), by=2)
> weekdays[-odder]
[[1]]
[1] "Mon"

[[2]]
[1] "Wed"

[[3]]
[1] "Fri"

So do you need to really search for dplyr functionality, such as how to take
all but some in a list  in a pipeline. use the odd function `[` to access
the subset.

> weekdays %>% `[`(-5)
[[1]]
[1] "Sun"

[[2]]
[1] "Mon"

[[3]]
[1] "Tue"

[[4]]
[1] "Wed"

[[5]]
[1] "Fri"

[[6]]
[1] "Sat"

> weekdays %>% `[`(odder)
[[1]]
[1] "Sun"

[[2]]
[1] "Tue"

[[3]]
[1] "Thu"

[[4]]
[1] "Sat"


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Christopher W. Ryan
via R-help
Sent: Thursday, November 18, 2021 4:40 PM
To: R-help using r-project.org
Subject: [R] the opposite of pluck() in purrr

I've just learned about pluck() and chuck() in the purrr package. Very cool!
As I understand it, they both will return one element of a list, either by
name or by [[]] index, or even "first"  or "last"

I was hoping to find a way to return all *but* one specified element of a
list. Speaking loosely, pluck(-1) or pluck(!1) or !pluck(1), but none of
those of course work. Thinking of English language, I had hopes for
chuck(1) as in "chuck element 1 away, leaving the rest"  but that's now how
it works.

Any tidyverse-centric ways to return all except one specified element of a
list?

Thanks.

--Chris Ryan

______________________________________________
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 http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list