title: R: convert nested for loop to apply function
body: <p>here is my minimal example:</p>
  
  <pre><code>calA_fun &lt;- function(list_A){
  for (k in 1:length(list_A)){
    out_level = list()
    out_level[[k]] &lt;- unlist( lapply(list_A[[k]], sqrt) )
    for (j in 1:length(list_A[[k]]) ){
        out_level_level = list()
        out_level_level[[j]] &lt;- lapply(list_A[[k]][[j]], function(x) x+ out_level[[k]] ) 
        print(out_level_level[[j]])
    }
  }
  }
  list_A &lt;- list(aa = c(1, 2, 4), bb = c(6,2))
  calA_fun(list_A)
  </code></pre>
  
  <p>I am rewriting this function using apply() family or map() to replace for loop but I don't know if it is possible since j is nested into k so it is a hierarchical structure.</p>
  
  <p>Thanks!</p>