[R] extract from a list of lists
Bill Dunlap
w||||@mwdun|@p @end|ng |rom gm@||@com
Tue Dec 27 23:27:03 CET 2022
I find your original sapply(List, function(element)element$name) easy to
understand. However replacing sapply with vapply makes for more robust
code. vapply requires you to supply the expected mode (type) and length of
element$name and if any of the elements don't comply with that, vapply
gives an error (sapply returns a list in this case). Also, for lists of
length zero, it returns a 0-long result of the mode you specified (instead
of sapply's list()). In the usual happy case it returns what sapply does.
It happens to be a bit faster than sapply and uses less memory, but I
think the checks that it does make it worth using instead of sapply.
E.g.,
> L <- list(list(A=1, B=c(TRUE,FALSE)), list(A=2, B=c(FALSE,FALSE)),
list(A=3))
> vapply(L, function(el)el$A, FUN.VALUE=numeric(1))
[1] 1 2 3
> vapply(L, function(el)el$B, FUN.VALUE=logical(2))
Error in vapply(L, function(el) el$B, FUN.VALUE = logical(2)) :
values must be length 2,
but FUN(X[[3]]) result is length 0
> vapply(L, function(el){ z <- el$B; if (is.null(z)) rep(NA,2) else z},
FUN.VALUE=logical(2))
[,1] [,2] [,3]
[1,] TRUE FALSE NA
[2,] FALSE FALSE NA
-Bill
On Tue, Dec 27, 2022 at 1:10 PM Therneau, Terry M., Ph.D. via R-help <
r-help using r-project.org> wrote:
> Thanks everyone for prodding my gray matter, which seems to be getting
> stiffer as I
> approach 70 (< 90 days).
>
> -- I'll continue to use the $ or [[ forms. That will suffice.
>
> -- I thought there might be a base R variant, e.g. something like
> extract( list,
> element-name); probably cross talk in my brain from the rstan library
>
> -- Gregg's note shows such a function in purr. But I rather like having
> as few
> dependencies as possible in a package, one usage is normally not enough,
> at least for
> something this simple.
>
> Terry T.
>
> On 12/27/22 14:38, Bert Gunter wrote:
> > Well, I prefer Greg's approach, but if you want to avoid calls to $ or
> > `[[` then you could do:
> >
> > unlist(fits)[ rep(names(fits[[1]]) == 'iter', length(fits))]
> >
> >
> > Cheers,
> > Bert
> >
> > On Tue, Dec 27, 2022 at 9:46 AM Greg Snow<538280 using gmail.com> wrote:
> >> Another option is the map family of functions in the purrr package
> >> (yes, this depends on another package being loaded, which may affect
> >> things if you are including this in your own package, creating a
> >> dependency).
> >>
> >> In map and friends, if the "function" is a string or integer, then it
> >> is taken as the piece to be extracted, so you should be able to do
> >> something like:
> >>
> >> library(purrr)
> >> map(fits, 'iter')
> >> # or
> >> map_int(fits, 'iter')
> >> # or
> >> map_dbl(fits, 'iter')
> >>
> >> which of the last 2 to use depends on how `iter` is stored.
> >>
> >> On Tue, Dec 27, 2022 at 10:16 AM Therneau, Terry M., Ph.D. via R-help
> >> <r-help using r-project.org> wrote:
> >>> I not uncommonly have the following paradym
> >>> fits <- lapply(argument, function)
> >>>
> >>> resulting in a list of function results. Often, the outer call is to
> mclapply, and the
> >>> function encodes some long calculation, e.g. multiple chains in an
> MCMC.
> >>> Assume for illustration that each function returns a list with
> elements beta, loglik, iter.
> >>>
> >>> Then sapply(fits, function(x) x$iter)
> >>> will give me a vector, with the number of iterations used by each
> instance.
> >>>
> >>> I've often been suspicious that there is some simple shorthand for the
> "grab all the
> >>> elements named iter" that skips the explicit x$iter function. Am I
> indeed overlooking
> >>> something? I don't expect a speed increase, just cleaner code.
> >>>
> >>> Terry T.
> >>>
> >>> --
> >>> Terry M Therneau, PhD
> >>> Department of Quantitative Health Sciences
> >>> Mayo Clinic
> >>> therneau using mayo.edu
> >>>
> >>> "TERR-ree THUR-noh"
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>>
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctherneau%40mayo.edu%7Cebbda887f4b94223376608dae84a58ae%7Ca25fff9c3f634fb29a8ad9bdd0321f9a%7C0%7C0%7C638077703266213025%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=wksTglkeQKmMSWWKHLsUijH5A25cH%2FuwxSNBOeNr9Sg%3D&reserved=0
> >>> PLEASE do read the posting guidehttps://
> nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctherneau%40mayo.edu%7Cebbda887f4b94223376608dae84a58ae%7Ca25fff9c3f634fb29a8ad9bdd0321f9a%7C0%7C0%7C638077703266213025%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9GASHCZVgnzNqAUrWimS7UphiqjYLOf50M1qWv6jeN0%3D&reserved=0
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
> >> --
> >> Gregory (Greg) L. Snow Ph.D.
> >> 538280 using gmail.com
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctherneau%40mayo.edu%7Cebbda887f4b94223376608dae84a58ae%7Ca25fff9c3f634fb29a8ad9bdd0321f9a%7C0%7C0%7C638077703266213025%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=wksTglkeQKmMSWWKHLsUijH5A25cH%2FuwxSNBOeNr9Sg%3D&reserved=0
> >> PLEASE do read the posting guidehttps://
> nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctherneau%40mayo.edu%7Cebbda887f4b94223376608dae84a58ae%7Ca25fff9c3f634fb29a8ad9bdd0321f9a%7C0%7C0%7C638077703266213025%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9GASHCZVgnzNqAUrWimS7UphiqjYLOf50M1qWv6jeN0%3D&reserved=0
> >> and provide commented, minimal, self-contained, reproducible code.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list