title: Summing values after every third position in data frame in R
body: <p>I am new to R. I have a data frame like following</p>
  
  <pre><code>&gt;df=data.frame(Id=c("Entry_1","Entry_1","Entry_1","Entry_2","Entry_2","Entry_2","Entry_3","Entry_4","Entry_4","Entry_4","Entry_4"),Start=c(20,20,20,37,37,37,68,10,10,10,10),End=c(50,50,50,78,78,78,200,94,94,94,94),Pos=c(14,34,21,50,18,70,101,35,2,56,67),Hits=c(12,34,17,89,45,87,1,5,6,3,26))
  
  Id       Start End Pos Hits
  Entry_1    20  50  14   12
  Entry_1    20  50  34   34
  Entry_1    20  50  21   17
  Entry_2    37  78  50   89
  Entry_2    37  78  18   45
  Entry_2    37  78  70   87
  Entry_3    68 200 101    1
  Entry_4    10  94  35    5
  Entry_4    10  94   2    6
  Entry_4    10  94  56    3
  Entry_4    10  94  67   26
  </code></pre>
  
  <p>For each entry I would like to iterate the data.frame in 3 different modes. For an example, for Entry_1 <code>mode_1 =seq(20,50,3)</code>and <code>mode_2=seq(21,50,3)</code> and <code>mode_3=seq(22,50,3)</code>. I would like sum all the Values in Column "Hits" whose corresponding values in Column "Pos" that falls in mode_1 or_mode_2 or mode_3 and generate a data.frame like follow:</p>
  
  <pre><code>Id       Mode_1   Mode_2   Mode_3
  Entry_1   0        17       34
  Entry_2   87       89        0
  Entry_3   1         0        0
  Entry_4   26        8        0 
  </code></pre>
  
  <p>I tried the following code:</p>
  
  <pre><code>    mode_1=0
      mode_2=0
      mode_3=0
      mode_1_sum=0
      mode_2_sum=0
      mode_3_sum=0
      for(i in dim(df)[1])
      {
        if(df$Pos[i] %in% seq(df$Start[i],df$End[i],3))
        {
        mode_1_sum=mode_1_sum+df$Hits[i]
        print(mode_1_sum)
        }
        mode_1=mode_1_sum+counts
        print(mode_1)
  
        ifelse(df$Pos[i] %in% seq(df$Start[i]+1,df$End[i],3))
        {
        mode_2_sum=mode_2_sum+df$Hits[i]
        print(mode_2_sum)
        }
        mode_2_sum=mode_2_sum+counts
        print(mode_2)
  
        ifelse(df$Pos[i] %in% seq(df$Start[i]+2,df$End[i],3))
        {
        mode_3_sum=mode_3_sum+df$Hits[i]
        print(mode_3_sum)
        }
        mode_3_sum=mode_3_sum+counts
        print(mode_3_sum)
  }
  </code></pre>
  
  <p>But the above code only prints 26. Can any one guide me how to generate my desired output, please. I can provide much more details if needed. Thanks in advance.</p>