Search code examples
rmax

Find x value corresponding to function peak/max in R


I want to find where a function peaks(x value) and not just what the peak value(y value) is, which can be easily done for a range of values, say p.

max(func(p))

Solution

  • Use either one. The 2nd one works if the same max value appears for many p.

    p[which.max(func(p))]
    
    p[which(func(p) == max(func(p)))]