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
tidyverse is an RStudio ecosystem of packages -- it is not part of R's "standard" package distro, so as the posting guide linked below says: "For questions about functions in standard packages distributed with R (see the FAQ Add-on packages in R <https://cran.r-project.org/doc/FAQ/R-FAQ.html#Add-on-packages-in-R>), ask questions on R-help. If the question relates to a *contributed package* , e.g., one downloaded from CRAN, try contacting the package maintainer first. You can also use find("functionname") and packageDescription("packagename") to find this information. *Only* send such questions to R-help or R-devel if you get no reply or need further assistance. This applies to both requests for help and to bug reports." While you may nevertheless get lucky here, a better bet is to post on RStudio's own forum, set up specifically to provide such help: https://community.rstudio.com/ Cheers, Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Nov 18, 2021 at 1:40 PM Christopher W. Ryan via R-help < r-help at r-project.org> wrote:> 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 at 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. >[[alternative HTML version deleted]]
On Thu, 18 Nov 2021 16:40:14 -0500 "Christopher W. Ryan via R-help" <r-help at r-project.org> wrote:> 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?Why (on earth!) hand-cuff yourself to someone else's prescription of how things should be done? Why not just do it yourself, especially given that it's so easy in this instance? Use mung[[1]] to get the first entry of a list named "mung". Or mung[1] of one if you want a *list* whose sole entry is the first entry of mung. Likewise mung[-1] will give you the "all but" results. Fewer key strokes, even. Tell tidyverse/purrr to pluck off! :-) cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
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 at r-project.org> On Behalf Of Christopher W. Ryan via R-help Sent: Thursday, November 18, 2021 4:40 PM To: R-help at 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 at 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.