title: how to avoid of using nested lapply in R?
body: <p>I am seeking efficient alternative for nested lapply, I think using nested structure is not appreciated in R community. Can anyone propose possible ideas, or approach to avoid of using nest lapply in custom function?</p>
  
  <p>Here is quick reproducible example:</p>
  
  <h1>simulated Data</h1>
  
  <pre><code>a &lt;- data.frame(
    start=seq(1, by=9, len=18), stop=seq(6, by=9, len=18),
    ID=letters[seq(1:18)], score=sample(1:25, 18, replace = FALSE))
  b &lt;- data.frame(
    start=seq(2, by=11, len=20), stop=seq(8, by=11, len=20),
    ID=letters[seq(1:20)], score=sample(1:25, 20, replace = FALSE))
  c &lt;- data.frame(
    start=seq(4, by=11, len=25), stop=seq(9, by=11, len=25),
    ID=letters[seq(1:25)], score=sample(1:25, 25, replace = FALSE))
  </code></pre>
  
  <h1>function that I used nested lapply, but want to avoid this:</h1>
  
  <pre><code>a.big &lt;- a[a$score &gt;10,]
  a.sml &lt;- a[(a$score &gt; 6 &amp; a$score &lt;= 10),]
  a.non &lt;- a[a$score &lt; 6,]
  
  a_new &lt;- list('big'=a.big, 'sml'=a.sml)
  tar.list &lt;- list(b,c)
  
  test &lt;- lapply(a_new, function(ele_) {
    re &lt;- lapply(tar.list, function(li) {
      out &lt;- base::setdiff(ele_, li)
      return(out)
    })
  })
  </code></pre>
  
  <h1>objective:</h1>
  
  <p>avoid of using nested lapply, to find its efficient alternative. I mean to find better representation for its output which must be easy/fast to reproduce, and allow fast/easy downstream computation. Is there any general approach to do this?</p>
  
  <p>How to avoid of using nested lapply in <code>test</code>? Can anyone propose possible ideas to get through this issues ? Thanks </p>
  
  <p>Best regards:</p>
  
  <p>Jeff</p>