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 at mayo.edu "TERR-ree THUR-noh" [[alternative HTML version deleted]]
Terry, I don't know if it is much cleaner or not, but you can use: sapply(fits, `[[`, 'iter') This calls the `[[` function (to extract list elements) on each element of the top list, with the extra argument of `iter` to say which element. On Tue, Dec 27, 2022 at 10:16 AM Therneau, Terry M., Ph.D. via R-help <r-help at 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 at mayo.edu > > "TERR-ree THUR-noh" > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
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 at 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 at mayo.edu > > "TERR-ree THUR-noh" > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com