title: R: Adress objects deep inside lists with filter commands inside functions/loops (ExtremeBounds package)
body: <p>I am using the ExtremeBounds package which provides as a result a multi level list with (amongst others) dataframes at the lowest level. I run this package over several specifications and I would like to collect some columns of selected dataframes in these results. These should be collected by specification (spec1 and spec2 in the example below) and arranged in a list of dataframes. This list of dataframes can then be used for all kind of things, for example to export the results of different specifications into different Excel Sheets.</p>
  
  <p>Here is some code which creates the problematic object (<strong>just run this code blindly, my problem only concerns how to deal with the kind of list it creates: eba_results</strong>):</p>
  
  <pre><code>library("ExtremeBounds")
  Data &lt;- data.frame(var1=rbinom(30,1,0.2),var2=rbinom(30,2,0.2),
                 var3=rnorm(30),var4=rnorm(30),var5=rnorm(30))
  spec1 &lt;- list(y=c("var1"), 
               freevars=c("var2"),
               doubtvars=c("var3","var4"))
  spec2 &lt;- list(y=c("var1"), 
            freevars=c("var2"),
            doubtvars=c("var3","var4","var5"))
  indicators &lt;- c("spec1","spec2")
  
  ebaFun &lt;- function(x){
  eba  &lt;- eba(data=Data, y=x$y, 
       free=x$freevars, 
       doubtful=x$doubtvars,
       reg.fun=glm, k=1, vif=7, draws=50, weights = "lri", family = binomial(logit))}
  
  eba_results &lt;- lapply(mget(indicators),ebaFun) #eba_results is the object in question
  </code></pre>
  
  <p>Manually I know how to access each element, for example:</p>
  
  <pre><code>eba_results$spec1$bounds$type #look at str(eba_results) to see the different levels
  </code></pre>
  
  <p>So "bounds" is a dataframe with identical column names for both spec1 and spec2. I would like to collect the following 5 columns from "bounds": </p>
  
  <p>type, cdf.mu.normal, cdf.above.mu.normal, cdf.mu.generic, cdf.above.mu.generic</p>
  
  <p>into one dataframe per spec. Manually this is simple but ugly:</p>
  
  <pre><code>collectedManually &lt;-list( 
  manual_spec1 = data.frame(
   type=eba_results$spec1$bounds$type, 
   cdf.mu.normal=eba_results$spec1$bounds$cdf.mu.normal,
   cdf.above.mu.normal=eba_results$spec1$bounds$cdf.above.mu.normal,
   cdf.mu.generic=eba_results$spec1$bounds$cdf.mu.generic,
   cdf.above.mu.generic=eba_results$spec1$bounds$cdf.above.mu.generic),
  manual_spec2= data.frame(
   type=eba_results$spec2$bounds$type, 
   cdf.mu.normal=eba_results$spec2$bounds$cdf.mu.normal,
   cdf.above.mu.normal=eba_results$spec2$bounds$cdf.above.mu.normal,
   cdf.mu.generic=eba_results$spec2$bounds$cdf.mu.generic,
   cdf.above.mu.generic=eba_results$spec2$bounds$cdf.above.mu.generic))
  </code></pre>
  
  <p>But I have more than 2 specifications and I think this should be possible with lapply functions in a prettier way. Any help would be appreciated!</p>
  
  <p><em>p.s.: A generic example to which hrbrmstr's answer applies but which turned out to be too simplistic:</em></p>
  
  <pre><code>exampleList = list(a=list(aa=data.frame(A=rnorm(10),B=rnorm(10)),bb=data.frame(A=rnorm(10),B=rnorm(10))),
                 b=list(aa=data.frame(A=rnorm(10),B=rnorm(10)),bb=data.frame(A=rnorm(10),B=rnorm(10))))
  </code></pre>
  
  <p><em>and I want to have an object which collects, for example, all the A and B vectors into two data frames (each with its respective A and B) which are then a list of data frames. Manually this would look like:</em></p>
  
  <pre><code>dfa &lt;- data.frame(A=exampleList$a$aa$A,B=exampleList$a$aa$B)
  dfb &lt;- data.frame(A=exampleList$a$aa$A,B=exampleList$a$aa$B)
  collectedResults &lt;- list(a=dfa, b=dfb)
  </code></pre>