title: Maximum first derivative in for values in a data frame R
body: <p>Good day, I am looking for some help in processing my dataset. I have 14000 rows and 500 columns and I am trying to get the maximum value of the first derivative for individual rows in different column groups. I have my data saved as a data frame with the first column being the name of a variable. My data looks like this:</p>
  
  <pre><code> Species   Spec400   Spec405   Spec410   Spec415
  1  AfricanOilPalm_1_Lf_1 0.2400900 0.2318345 0.2329633 0.2432734
  2 AfricanOilPalm_1_Lf_10 0.1783162 0.1808581 0.1844433 0.1960315
  3 AfricanOilPalm_1_Lf_11 0.1699646 0.1722618 0.1615062 0.1766804
  4 AfricanOilPalm_1_Lf_12 0.1685733 0.1743336 0.1669799 0.1818896
  5 AfricanOilPalm_1_Lf_13 0.1747400 0.1772355 0.1735916 0.1800227
  </code></pre>
  
  <p>For each of the variables in the species column, I want to get the maximum derivative from Spec495 to Spec500 for example. This is what I did before I ran into errors.</p>
  
  <pre><code>x&lt;-c(495,500,505,510,515,520,525,530,535,540,545,550)##get x values of     reflectance(Spec495 to Spec500)
  
  y.data.f&lt;-hsp[,21:32]##get row values for the required columns
  
  y&lt;-as.numeric(y.data.f[1,])##convert to a vector, for just the first row of data
  
  library(pspline) ##Using a spline so a derivative maybe calculated from a list of   numeric values
  </code></pre>
  
  <p>I really wanted to avoid using a loop because of the time it takes, but this is the only way I know of thus far</p>
  
  <pre><code>for(j in 1:14900)
  + { y&lt;-as.numeric(y.data.f[j,]) + a1d&lt;-max(predict(sm.spline(x, y), x, 1))
  +     write.table(a1d, file = "a1-d-appended.csv", sep = ",", 
  + col.names = FALSE,   append=TRUE) + }
  </code></pre>
  
  <p>This loop runs up until the 7861th value then get this error:</p>
  
  <pre><code>Error in smooth.Pspline(x = ux, y = tmp[, 1], w = tmp[, 2], method = method,  : 
  NA/NaN/Inf in foreign function call (arg 6)
  </code></pre>
  
  <p>I am sure there must be a way to avoid using a loop, maybe using the plyr package, but I can't figure out how to do so, nor which package would be best to get the value for maximum derivative.</p>
  
  <p>Can anyone offer some insight or suggestions? Thanks in advance</p>