title: Build a table by pasting rows extracted from subsets based on a value
body: <p>I have daily stock data from 1970 to 2019 subset by year. My goal is to get the date that the minimum occurred in each year in one column and the minimum value in another column. I can iterate through the subsets to get the minimum of each year using "foreach", ".combine=rbind", "lapply" and "which.min" to build a list of the rows that I want but I can't get the date out of the index this way.   </p>
  
  <pre class="lang-r prettyprint-override"><code>    mindates &lt;- foreach(i = 1:length(GSPC_yearly), .combine=rbind) %do% {
      # I would like to be able to attach the corresponding row label date from the index to this code
            spyannmin&lt;-as.numeric(lapply(GSPC_yearly[[i]]$GSPC.Low,min))
            spyannmindate&lt;-(lapply(GSPC_yearly[[i]]$GSPC.Low,which.min))
  
       # Or be able to bind this code row wise because its output already includes the row label date from the index of the source data.  This is only giving me the result of the last [[i]], i want a table of rows with all the [[i]]'s       
             spyannmindexdate&lt;-GSPC_yearly[[i]][spyannmindate,3]
  
        result.data&lt;-c(spyannmin, spyannmindate,spyannmindexdate)
             }
  
  head(mindates,3)
  spyannmindexdate
  </code></pre>
  
  <p>This gives me output such as...</p>
  
  <pre><code>                    GSPC.Low       
  result.1 68.61  101      68.61 
  result.2 89.34  227      89.34 
  result.3 100.87 2        100.87
  # But I would like the date to appear where the result.# appears or in a new column, I'm not sure which would be better.
  
             GSPC.Low
  2019-01-03  2443.96
  # Or I would like this data exactly, but pasted with the respective row from each yearly subset of the larger source data.  Again I want output that  includes the row for each year.
  </code></pre>
  
  <p>If I use just "which.min" to get the row index then I CAN get the date and the minimum value but without "lapply" I don't know how to build the table with ".combine=rbind" </p>
  
  <p>So I have two approaches to my problem but am missing a key ingredient in each.  I would appreciate a solution to either. Solutions to both would help advance my understanding of r programing.  Thank you in advance.</p>