Compute histrogram bins for a large data.frame fast
Within the algorithm you post you can take advantage of a few features:


if(bins[j]&lt;=convertedDates[i]&amp;&amp;convertedDates[i]&lt;bins[j+1]) - as you check the bins in order there is no need to check that the previous bin.


As explanation say if j is 5 then you check bins[5]&lt;=convertedDates[i]&amp;&amp;convertedDates[i]&lt;bins[6]
But in the next step j is 6 and you check. bins[6]&lt;=convertedDates[i]&amp;&amp;convertedDates[i]&lt;bins[7]

So you twice compare with bins[6] - if you think about it you only reach j=6 if bins[6]&lt;=convertedDates[i]. So no need to check that again.


if you would sort the convertedDates before the comparison you can use that you can start the search of bins by the last found bin - instead of the first.
actually there is no need to loop over all bins - instead if you calculate (convertedDates - absStart) %/% input$binning it should tell you exactly which bin the date belongs.
it may also help slightly if you preallocate the output lists as shown here.


It might also help to read the source code of the hist function and see what "tricks" they use there.