how do i fit unique curves on each unique plot in a for loop <p>I have written this code (see below) for my data frame <code>kleaf.df</code> to combine multiple plots of variable <code>press_mV</code> with each individual plot for unique <code>ID</code></p> <p>I need some help fitting curves to my plots. when i run this code i get the same fitted curve (the curve fitted for the first plot) on ALL the plots where i want each unique fitted curve on each unique plot.</p> <p>thanks in advance for any help given</p> <pre><code>f &lt;- function(t,a,b) {a * exp(b * t)} par(mfrow = c(5, 8), mar = c(1,1,1,1), srt = 0, oma = c(1,6,5,1)) for (i in unique(kleaf.df$ID)) { d &lt;- subset(kleaf.df, kleaf.df$ID == i) plot(c(1:length(d$press_mV)),d$press_mV) #----tp:turning point. the last maximum value before the values start to decrease tp &lt;- tail(which( d$press_mV == max(d$press_mV) ),1) #----set the end points(A,B) to fit the curve to A &lt;- tp+5 B &lt;- A+20 #----t = time, p = press_mV # n.b:shift by 5 accomadate for the time before attachment t &lt;- A:B+5 p &lt;- d$press_mV[A:B] fit &lt;- nls(p ~ f(t,a,b), start = c(a=d$press_mV[A], b=-0.01)) #----draw a curve on plot using the above coefficents curve(f(x, a=co[1], b=co[2]), add = TRUE, col="green", lwd=2) } </code></pre>