0

everyone I am new to R. I would like to find out the first and second maximums and their locations, using R studio. It would be appreciate if you could help me how to do that.

Example explanation:

Example trend

I would like to find the two maximums (before and after depression - lowest value). In this picture, the first maximum will be 17 before the depression (lowest value), and the second maximum will be 16 after the depression (lowest value). Also, I would like to find out their locations on x-axis. The first maximum 17 will be on 5, and the second maximum will be on 11.

Heiwa
  • 41
  • 4
  • 1
    Welcome to StackOverflow! It will help if you can make your post reproducible by including some sample data. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info on how to construct a high quality post to get good help on your problem. – Dan Adams Apr 07 '22 at 03:24
  • Are you just interested in the top two `y` values and the corresponding `x` values at which they occur? – Dan Adams Apr 07 '22 at 03:26
  • Yes, I would like to get top two y values and their corresponding x values. @DanAdams – Heiwa Apr 07 '22 at 04:19
  • Assuming a dataset `d <- data.frame(x = 1:20, y = runif(20, 5, 20))`, you can just use `d[order(d$y, decreasing = T)[1:2],]` which basically sorts in decreasing order by `y` and returns the top `2` rows. – Dan Adams Apr 07 '22 at 04:24
  • But, in my dataset, some top two y-values are on the same side of the depression. I would like to get one top y-values before the depression (lowest point) and another top y-value after the depression. – Heiwa Apr 07 '22 at 04:54
  • Ok that's more complicated. Please edit your question by clearly explaining your criteria and add some sample data. You need to be pretty explicit about how you would mathematically define the 'depression'. – Dan Adams Apr 07 '22 at 04:58
  • I am sorry I made you confused. I already edited my post. Thanks, @DanAdams – Heiwa Apr 07 '22 at 05:05

0 Answers0