title: Finding median of values in one column given corresponding consecutive values in another column are equal numbers
body: <p>I have a data frame that looks like this in R:</p>
  
  <pre><code>Date | Time | value  
  A  | 1  | 3       
  A  | 1  | 6     
  A  | 2  | 4  
  A  | 3  | 3  
  A  | 4  | 2  
  A  | 5  | 7  
  B  | 1  | 6       
  B  | 2  | 5  
  B  | 2  | 3  
  B  | 2  | 4  
  B  | 3  | 2  
  B  | 5  | 3  
  B  | 6  | 4  
  ... 
  </code></pre>
  
  <p>My aim is to find the median of the numbers in the third column if the numbers in the second columns are equal in consecutive rows. I.e take the median of the values if they occur at the same time, and replacing that median value for that corresponding time slot.</p>
  
  <p>So output I am aiming for:</p>
  
  <pre><code>A  | 1  | median (3,6)       
  A  | 2  | 4  
  A  | 3  | 3  
  A  | 4  | 2  
  A  | 5  | 7  
  B  | 1  | 6       
  B  | 2  | median (3,4,5)   
  B  | 3  | 2  
  B  | 5  | 3  
  B  | 6  | 4  
  ... 
  </code></pre>
  
  <p>I am desperately trying to avoid loops since the data set is huge. The main problem I am having is collecting the values separately. This is what I had thus far:</p>
  
  <pre><code>#First find consecutive time slots that are equal:
  timeslots_equal&lt;-which(diff(data_RAW$TIME)==0)
  
  coordinates_placesholder &lt;- sort(c(as.vector(timestamp_equal_coordinates), as.vector(timestamp_equal_coordinates)+1))
  
  coordinates_placesholder2  &lt;-  coordinates_placesholder[-c(which(diff(coordinates_placesholder)==0), which(diff(coordinates_placesholder)==0) +1)]
  
   #The following matrix are the coordinates in the value vector with equal time slots
   matrix_ranges&lt;-t(matrix(coordinates_placesholder2,2))
  </code></pre>
  
  <p><code>matrix_ranges</code> for the example above would look like:  </p>
  
  <pre><code>1 | 2  
  8 | 10  
  </code></pre>
  
  <p>Then I tried to apply something like </p>
  
  <pre><code>median(data_RAW$Value[matrix_ranges[,1]:matrix_ranges[,2]])
  </code></pre>
  
  <p>This did not work. Does anyone have any answers on doing this?</p>
  
  <p>Also is there an easier way to doing this than what I did above?</p>