title: Finding maximum data with corresponding value from other column in R
body: <p>I have daily data (4011 days) together with indicator (1-weekdays, 2-weekend). I want to find weekly maxima with the corresponding indicator. For example (let say) the data is:</p>
  
  <pre><code>mydat &lt;- matrix(c(0.027,0.034,0.019,0.021,0.026,0.024,0.058,0.026,0.064,
       0.066,0.026,0.101,0.069,0.054,rep(2,2),rep(1,5),rep(2,2),rep(1,5)), ncol=2)
  </code></pre>
  
  <p>I have try with the following code. I manage to get the maximum sequences (in this case, weekly maxima) but  I dont want maximum sequences in indicator. Here is the code</p>
  
  <pre><code>week.max &lt;- function(vec){    
     if(length(vec[is.na(vec)]) == 7){       
     return(NA)     
     } 
     else{
       return(max(vec, na.rm = T))
     } 
  }
  
  max.week.dat &lt;- apply(mydat, 2, function(x) tapply(x, rep(1:(length(x)/7), 
    each=7, len=length(x)), week.max)) 
  </code></pre>
  
  <p>and the result</p>
  
  <pre><code>matrix(c(0.058,0.101,2,2),ncol=2)
  </code></pre>
  
  <p>I want the output like this:</p>
  
  <pre><code>matrix(c(0.058,0.101,1,1),ncol=2)
  </code></pre>
  
  <p>Many thanks in advance.</p>