[R] extract from a list of lists

Therneau, Terry M., Ph.D. therne@u @end|ng |rom m@yo@edu
Tue Dec 27 18:06:04 CET 2022


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]]



More information about the R-help mailing list