title: Calculating the peak function for group wise(Tag) in a data frame and further Rbinding it into new data frame
body: <p>I am new at asking questions on Stack, so please pardon me if I get it wrong. Here is the scenario (I have tried to reproduce it with a simple example): </p>
  
  <pre><code>library("pracma") 
  Tag&lt;- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
      3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,5, 5,
      6, 6, 6, 6, 6, 6, 6, 6, 6, 6)
  Temp&lt;- c(43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41, 43, 44, 39,38,
       37, 43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41, 43, 44, 
       39, 38, 37, 43, 44, 45, 41, 43, 38, 40, 41, 39, 37, 37, 39, 45, 42, 41,43,
       44, 39, 38, 37)
  dfr=data.frame(Tag=Tag,Temp=Temp)
  </code></pre>
  
  <p><strong>DATA Description</strong> - We have two columns:<br>
  <strong>Tag</strong> [group wise variable]<br>
  <strong>Temp</strong> (numerical variable where peak function has to be performed)
  <br></p>
  
  <pre><code>for (i in 1:6) {
  
  df=filter(dfr , dfr$Tag == i)
  pik =findpeaks(df$Temp, nups = 1, ndowns = 0, zero = "+", peakpat = NULL,
               minpeakheight = 33, minpeakdistance = 4,
               threshold =0.42, npeaks = 11, sortstr = FALSE)#Peak Function
  
  pik&lt;- as.data.frame(pik)#Converting into data frame as it is in matrix form
  names(pik) &lt;- c("Temp","Peak_Mid","Peak_start","Peak_End")# renaming the header
  pik &lt;- arrange(pik , Peak_Mid)#Rearranging with Peak_Mid
  attach(pik)#attaching pik df 
  j=1#initializing for loop
  s=0#initializing for loop
  for (j in 1:nrow(pik))#for loop for calculating slope individual points
  s[[j]]=((Temp[j+1]-Temp[j])/(Peak_Mid[j+1]-Peak_Mid[j]))
  pik$Trend &lt;- 0#creating new column(Trend) filled with zero
  pik$Trend &lt;- s# inserting the calculated s variable onto pik df 
  w[[i]]=as.data.frame(pik) 
  }
  </code></pre>
  
  <p>I was trying to turn the above code into a <code>for</code> loop, such that at every <code>i</code>th value i:e (<code>Tag[i]</code> in our case <code>i</code> ranges from 1 to 6 as per our data). So every time for <code>Tag[i]</code> will compute through peak function and then we will calculate the slope among the points and we will get a new data frame with 4 columns.</p>
  
  <p>This computation will be performed on each <code>Tag[i]</code> which is a subset of main data frame. So, we will get <code>i</code> different data frames, these data frame will be rbind with tag no along with it.</p>
  
  <p>This is visual of input with the expected output:</p>
  
  <p><a href="https://i.stack.imgur.com/b8kWe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b8kWe.png" alt=""></a></p>