Hello,<br>I need to create a list and then fill it sequentially by adding components in a for loop. Here is an example that works:<br><br>library(inline)<br>src &lt;- &#39;<br>Rcpp::List mylist(2);<br>for(int i=0; i&lt;2; ++i)<br>
  mylist[i] = i;<br>mylist.names() = CharacterVector::create(&quot;a&quot;,&quot;b&quot;);<br>return mylist;<br>&#39;<br>fun &lt;- cxxfunction(body=src, plugin=&quot;Rcpp&quot;)<br>print(fun())<br><br>But what I really want is to create an empty list and then fill it, that is without specifying its number of components before hand... This is because I don&#39;t know in advance at which step of the for loop I will need to create a new component. Here is an example, that obviously doesn&#39;t work, but that should show what I am looking for:<br>
<br>Rcpp::List mylist;<br>CharacterVector names = CharacterVector::create(&quot;a&quot;, &quot;b&quot;);<br>
for(int i=0; i&lt;2; ++i){<br>  mylist.add(names[i], IntegerVector::create());<br>
  mylist[names[i]].push_back(i);<br>}<br>
return mylist;<br>
<br>Do you know how I could achieve this? Thanks.<br>