title: Append to list of lists in for loop
body: <p>I have a two data frames:</p>
  
  <pre><code>data&lt;-structure(list(sample = structure(c(1L, 2L, 2L, 1L, 1L, 1L, 1L,
                                                2L, 2L, 2L), .Label = c("S1", "S2"), class = "factor"), chrom = structure(c(1L,
                                                                                                                            1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L), .Label = c("2L", "2R"), class = "factor"),
                           pos = c(318351L, 605574L, 1014043L, 2031592L, 2886957L, 2910379L,
                                   2218351L, 105574L, 1344043L, 216957L)), .Names = c("sample",
                                                                                      "chrom", "pos"), row.names = c(NA, 10L), class = "data.frame")
  
  &gt; arrange(data, chrom,sample)
  
     sample chrom     pos
  1      S1    2L  318351
  2      S1    2L 2218351
  3      S2    2L  605574
  4      S2    2L 1014043
  5      S2    2L  105574
  6      S2    2L 1344043
  7      S1    2R 2031592
  8      S1    2R 2886957
  9      S1    2R 2910379
  10     S2    2R  216957
  </code></pre>
  
  <hr>
  
  <pre><code>svBreaks&lt;-structure(list(sample = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L,
                                                1L, 2L, 1L), .Label = c("S1", "S2"), class = "factor"), chrom = structure(c(1L,
                                                                                                                            1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("2L", "2R"), class = "factor"),
                           bp = c(2425901L, 2426025L, 6694426L, 6694566L, 8387755L,
                                  8387927L, 8963713L, 963799L, 980364L, 980521L), gene = structure(c(3L,
                                                                                                     3L, 5L, 5L, 4L, 4L, 2L, 2L, 1L, 1L), .Label = c("CG8213",
                                                                                                                                                     "CG8216", "intergenic", "pdm3", "Tsp"), class = "factor"),
                           type = structure(c(2L, 1L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 3L
                           ), .Label = c("DEL", "DUP", "INV", "TANDUP"), class = "factor")), row.names = c(NA,
                                                                                                           10L), .Names = c("sample", "chrom", "bp", "gene", "type"), class = "data.frame")
  
  &gt; arrange(svBreaks, chrom, sample)
  
   sample chrom      bp       gene   type
  1      S1    2L 2425901 intergenic    DUP
  2      S1    2L 2426025 intergenic    DEL
  3      S1    2L 6694426        Tsp    DUP
  4      S2    2L 6694566        Tsp    DEL
  5      S2    2L 8387755       pdm3    INV
  6      S2    2L 8387927       pdm3    INV
  7      S1    2R 8963713     CG8216    INV
  8      S1    2R  963799     CG8216 TANDUP
  9      S1    2R  980521     CG8213    INV
  10     S2    2R  980364     CG8213 TANDUP
  </code></pre>
  
  <p>And a function that calculates the distance between <code>pos</code> in <code>data</code> and <code>bp</code> in <code>svBreaks</code> on the same <code>chrom</code> for each sample:</p>
  
  <pre><code>fun3 &lt;- function(p) {
    index&lt;-which.min(abs(sv_df$bp - p))
    closestBp&lt;-as.numeric(sv_df$bp[index])
    chrom&lt;-as.character(sv_df$chrom[index])
    gene&lt;-as.character(sv_df$gene[index])
    sample&lt;-as.character(sv_df$sample[index])
    type&lt;-as.character(sv_df$type[index])
  
    dist&lt;-(p-closestBp)
    list(p, closestBp, dist, chrom, gene, type, sample)
  }
  </code></pre>
  
  <p>I want to loop through each <code>chrom</code>, and call the function for each <code>sample</code> and build a data frame containing all <code>sample</code>s and <code>chrom</code>s. Here's what I've got so far:</p>
  
  <pre><code>l &lt;- list()
  
  for (c in levels(data$chrom)){
    for (s in levels(data$sample)){
      # Filter in chromosome and sample
      df&lt;-filter(data, chrom == c &amp; sample == s)
      sv_df&lt;-filter(svBreaks, chrom == c &amp; sample == s)
  
      # Apply funciton
      dist2bp&lt;-lapply(df$pos, fun3)
  
      dist2bp&lt;-do.call(rbind, dist2bp)
      dist2bp&lt;-as.data.frame(dist2bp)
  
      colnames(dist2bp)=c("snp", "closest_bp", "min_dist", "chrom", "closest_gene", "type", "sample")
      l[[s]] &lt;- dist2bp
    }
  }
  </code></pre>
  
  <p>However, this does not give me the expected output, and only saves the second level of <code>chrom</code> - here's the resulting data frame:</p>
  
  <pre><code>&gt; levels(data$chrom)
  [1] "2L" "2R"
  &gt; levels(data$sample)
  [1] "S1" "S2"
  
           snp closest_bp min_dist chrom closest_gene   type sample
  S1.1 2031592     980521  1051071    2R       CG8213    INV     S1
  S1.2 2886957     980521  1906436    2R       CG8213    INV     S1
  S1.3 2910379     980521  1929858    2R       CG8213    INV     S1
  S2    216957     980364  -763407    2R       CG8213 TANDUP     S2
  </code></pre>
  
  <hr>
  
  <p>If I also add to the list in the <code>chrom</code> loop, the output is also not correct: </p>
  
  <pre><code>for (c in levels(data$chrom)){
    for (s in levels(data$sample)){
      [...]
      l[[s]] &lt;- dist2bp
    }
    l[[c]] &lt;- dist2bp
  }
  
  snp closest_bp min_dist chrom closest_gene   type sample
  S1.1 2031592     980521  1051071    2R       CG8213    INV     S1 # Chrom level 2, S1
  S1.2 2886957     980521  1906436    2R       CG8213    INV     S1
  S1.3 2910379     980521  1929858    2R       CG8213    INV     S1
  S2    216957     980364  -763407    2R       CG8213 TANDUP     S2 # Chrom level 2, S2
  2L.1  605574    6694566 -6088992    2L          Tsp    DEL     S2 # Chrom level 1, S2
  2L.2 1014043    6694566 -5680523    2L          Tsp    DEL     S2
  2L.3  105574    6694566 -6588992    2L          Tsp    DEL     S2
  2L.4 1344043    6694566 -5350523    2L          Tsp    DEL     S2
  2R    216957     980364  -763407    2R       CG8213 TANDUP     S2 # Not sure why this is here, # Chrom level 1, S1 is missing
  </code></pre>
  
  <p>Can anyone point me in the right direction?</p>