Is there a way to speed-up the following code? Is there a way to speed-up the following R code?

actionVec&lt;-NULL
for(iii in 1:length(resultTable$V1)){
actionVec[iii]&lt;- sum(browse$V2==resultTable$V1[iii] &amp; browse$V4 == i &amp; browse$V5 == ii)}


this is a loop to form a vector for each record (row) in resultTable, containing the number of entries that match the conditions in table browse

this script runs 36 minutes. I suspect that there is a way to make it run much faster. I have experienced a reduction from 15 min to 10 ms (!) on another script when avoiding loops but using vectors. But in this case I can`t find out how to make it.

EDIT1:
variables i and ii come from outer loop and may be two arbitrary integers for this loop.

EDIT2: more complete version

SESSION_NUMBER&lt;-length(resultTable$V1)

for(i in 1:55){ for(ii in 1:21){  
if(sum(browse$V4 == i &amp; browse$V5 == ii) &gt; treshold){
actionVec&lt;-NULL
for(iii in 1:SESSION_NUMBER){
actionVec[iii]&lt;- sum(browse$V2==resultTable$V1[iii] &amp; browse$V4 == i &amp; browse$V5 == ii)}
resultTable[,i+1] &lt;- actionVec
}}}
Yes, there is:

Since i and ii and browse$V4 and browse$V5 are outside of the loop, you can precalculate this before going into the loop. That way you can save a small bit of calculating.

if (sum(browse$V4 == i &amp; browse$V5 == ii) &gt; treshold)

I dunno what you're doing here, but from what I can see, you're calling sum(1) or sum(0). Naturally, this produces 0 or 1. Then you're comparing it to treshold. Well, if treshold is below 1 but not below 0, then you can just remove the check, as well as the for loop, since browse doesn't change in the for loop!

If treshold is below 0, then the check is not needed. Thus, (please check if my syntax is correct)

SESSION_NUMBER&lt;-length(resultTable$V1)

if(treshold &lt; 1 &amp;&amp; treshold &gt;= 0){
    //check if browse$V4 and browse$V5 are in range
    if(browse$V4 &gt;= 1 &amp;&amp; browse$V4 &lt;= 55 &amp;&amp; browse$V5 &gt;= 1 &amp;&amp; browse$V5 &lt;= 21){
        actionVec&lt;-NULL
        for(iii in 1:SESSION_NUMBER){
            actionVec[iii]&lt;- sum(browse$V2==resultTable$V1[iii])
        }
        resultTable[,browse$V4+1] &lt;- actionVec
    }
} else if (treshold &lt; 0){
    for(i in 1:55){ 
        for(ii in 1:21){  
            actionVec&lt;-NULL
            temp&lt;-browse$V4 == i &amp; browse$V5 == ii
            for(iii in 1:SESSION_NUMBER){
                actionVec[iii]&lt;- sum(browse$V2==resultTable$V1[iii] &amp; temp)
            }
            resultTable[,i+1] &lt;- actionVec
        }
    } 
}