title: "Error in `[[<-`(`*tmp*`, i, value = ) : no such index at level 1" assigning for loop result to list
body: <p>I have a list of numerical vectors. For each vector I want to determine the x-values for the kernal density peaks using a for loop. Running the for loop when printing the results works fine. When trying to store the results in a list I get the following error: </p>
  
  <blockquote>
    <p>"Error in <code>[[&lt;-</code>(<code>*tmp*</code>, i, value = d$x[c(F, diff(diff(d$y) >= 0) &lt;
    0)]) : no such index at level 2" </p>
  </blockquote>
  
  <p>The Error message in my original code</p>
  
  <blockquote>
    <p>returns: "... no such index at level 1".</p>
  </blockquote>
  
  <p>Can anyone help me fixing this error? The code for extracting the x-values for the kernal density peaks came from <a href="https://stackoverflow.com/questions/13133297/calculating-peaks-in-histograms-or-density-functions">Calculating peaks in histograms or density functions</a>, third answer.</p>
  
  <pre><code>set.seed(1234)
  
  x &lt;- list(col1 = c(rnorm(100, mean = 3), rnorm(100, mean = 4)),
            col2 = c(rnorm(100, mean = 3), rnorm(100, mean = 4)))
  
  # Works fine
  output &lt;- vector("list", length(x))
  for (i in (x)){
  d &lt;- density(i)
  d$x[c(F, diff(diff(d$y) &gt;= 0) &lt; 0)] %&gt;% print()
  }
  
  # Does not work
  output &lt;- vector("list", length(x))
  for (i in (x)){
  d &lt;- density(i)
  d$x[c(F, diff(diff(d$y) &gt;= 0) &lt; 0)] -&gt; output[[i]]
  }
  </code></pre>