title: How to get index of max of each row every three columns of a data frame?
body: <p>I would like to find the index of the max for each row of my ldf dataframe every three columns and create a dataframe with the results.
  Here below my code that works.
  But, I would like to improve it and make it faster. I have tried to use apply to go throgh the rows of ldf but did not succeeded.</p>
  
  <p>Here my failure:</p>
  
  <pre><code>ldf&lt;-data.frame(name=c("aa","bb","cc"),V1=c(10,15,10),V2=c(20,10,8),
  V3=c(8,8,9),V4=c(20,8,9),V5=c(10,15,8),V6=c(8,10,10))
  pos&lt;-NULL
  index&lt;-NULL
  pos&lt;-apply(ldf,1,function(x){
  index&lt;-sapply(seq(2, length(x), 3),function(i){
  index&lt;-NULL
  cbind(index,which(x[i:(i+2)]==max(x[i:(i+2)]), arr.ind=TRUE)[2])
  
  })
  pos&lt;-rbind(pos,index)
  })
  
  pos
       [,1] [,2] [,3]
  [1,]   NA   NA   NA
  [2,]   NA   NA   NA
  </code></pre>
  
  <p>Here the code that works with the expected results to be improved:</p>
  
  <pre><code>ldf&lt;-data.frame(name=c("aa","bb","cc"),V1=c(10,15,10),V2=c(20,10,8),
  V3=c(8,8,9),V4=c(20,8,9),V5=c(10,15,8),V6=c(8,10,10))
  index&lt;-NULL
  pos&lt;-NULL
  
  
  for(x in 1:dim(ldf)[1]){
  index&lt;-sapply(seq(2, dim(ldf)[2], 3),function(i){
  index&lt;-NULL
  cbind(index,which(ldf[x,i:(i+2)]==max(ldf[x,i:(i+2)]), 
      arr.ind=TRUE)[2])
  
      })
  pos&lt;-rbind(pos,index)
  }
  pos
        [,1] [,2]
  index    2    1
  index    1    2
  index    1    3
  </code></pre>
  
  <p>Thanks in advance!</p>