How to get the peak values in y-axis and the corresponding values of x-axis?

Dear R users,
I'm working on the dataset, and trying to get the peak values of y. In the dataframe df1, the y column includes 120 values, but I do not know how to separate each value with comma when export the data.

df1= data.frame(x=1:120,
y='0.000404607994364647
0.000418386229955128
0.000430794394524755
0.000441608763817604
0.000450720069991295
0.000458353928139761
0.000465244320438098
0.000472605442408119
0.000481836517062748
0.000494031258256718
0.000509477897425229
0.000527386888743664
0.000546053806503485
0.000563536347150758
0.000578694693557497
0.000592184934692638
0.000606880432664763
0.00062740434643328
0.000658951780340787
0.000706022263900694
0.000771695560505854
0.000857665701641296
0.000964821690647073
0.00109410977624778
0.00124762020201182
0.00142985018853286
0.00164871137836403
0.00191549532233234
0.00224333357803463
0.00264484132738258
0.00313097018738546
0.00371357432026318
0.00441311650708302
0.00527051763162742
0.00635951029446201
0.00779442118237125
0.0097289184514785
0.0123436051467147
0.0158230846458223
0.0203248591277636
0.0259430773877464
0.0326716119103998
0.0403751637620159
0.0487822072022065
0.0575125639221519
0.0661386623990792
0.0742571414246157
0.0815347852470562
0.0877087303679977
0.0925592583028204
0.0958964121231122
0.097577846789136
0.0975299735518198
0.0957428778093581
0.0922662141274413
0.0872674196185096
0.0811466237435986
0.0745892159235215
0.0684282888234455
0.0633408656175527
0.0595754289702118
0.0569149855310436
0.0549012076098515
0.0531548543217289
0.0515892280330073
0.0504152091574836
0.0499599635705786
0.0504031217012216
0.0515799825745852
0.0529930988557304
0.0540733795338386
0.0545753160201401
0.0548889928851148
0.0560788146361983
0.0595912907883012
0.0667366977351586
0.0781688004268402
0.0935992678733076
0.111863211025667
0.131261468491735
0.14996894361554
0.16630408902095
0.178792397549176
0.186142519356196
0.187370254991816
0.182238791895045
0.171908697734763
0.159376822267072
0.149232179773896
0.14661852192979
0.15582462684655
0.179167102876311
0.216598366309568
0.26603216591513
0.324083541154487
0.386820517224485
0.450113246364987
0.509323652377758
0.558573294272359
0.590463672842883
0.597264437175175
0.573745920439652
0.520380960986617
0.444758790690755
0.359750166225963
0.278987238175495
0.21206887381186
0.162091637731931
0.12651426193784
0.100279095709724
0.0790468365289463
0.0608671439409344
0.0459482418843583
0.0353311402777704
0.0295872989249962
0.0282323900120659
0.0298985530326881
0.0328961840601709
0.0357555698838089
0.0375239368848331')
plot(df1$x,df1$y)

Anyway, the output figure should be like this. There are three peak values of y, how to get the peak values and the corresponding three x values? Thanks for your help.Rplot

Looks like a simple syntax problem:

df1 <- data.frame(
  x = 1:120,
  y = c(
    0.000404607994364647,
    0.000418386229955128,
    0.000430794394524755,
    0.000441608763817604,
    0.000450720069991295,
    0.000458353928139761,
    0.000465244320438098,
    0.000472605442408119,
    0.000481836517062748,
    0.000494031258256718,
    0.000509477897425229,
    0.000527386888743664,
    0.000546053806503485,
    0.000563536347150758,
    0.000578694693557497,
    0.000592184934692638,
    0.000606880432664763,
    0.00062740434643328,
    0.000658951780340787,
    0.000706022263900694,
    0.000771695560505854,
    0.000857665701641296,
    0.000964821690647073,
    0.00109410977624778,
    0.00124762020201182,
    0.00142985018853286,
    0.00164871137836403,
    0.00191549532233234,
    0.00224333357803463,
    0.00264484132738258,
    0.00313097018738546,
    0.00371357432026318,
    0.00441311650708302,
    0.00527051763162742,
    0.00635951029446201,
    0.00779442118237125,
    0.0097289184514785,
    0.0123436051467147,
    0.0158230846458223,
    0.0203248591277636,
    0.0259430773877464,
    0.0326716119103998,
    0.0403751637620159,
    0.0487822072022065,
    0.0575125639221519,
    0.0661386623990792,
    0.0742571414246157,
    0.0815347852470562,
    0.0877087303679977,
    0.0925592583028204,
    0.0958964121231122,
    0.097577846789136,
    0.0975299735518198,
    0.0957428778093581,
    0.0922662141274413,
    0.0872674196185096,
    0.0811466237435986,
    0.0745892159235215,
    0.0684282888234455,
    0.0633408656175527,
    0.0595754289702118,
    0.0569149855310436,
    0.0549012076098515,
    0.0531548543217289,
    0.0515892280330073,
    0.0504152091574836,
    0.0499599635705786,
    0.0504031217012216,
    0.0515799825745852,
    0.0529930988557304,
    0.0540733795338386,
    0.0545753160201401,
    0.0548889928851148,
    0.0560788146361983,
    0.0595912907883012,
    0.0667366977351586,
    0.0781688004268402,
    0.0935992678733076,
    0.111863211025667,
    0.131261468491735,
    0.14996894361554,
    0.16630408902095,
    0.178792397549176,
    0.186142519356196,
    0.187370254991816,
    0.182238791895045,
    0.171908697734763,
    0.159376822267072,
    0.149232179773896,
    0.14661852192979,
    0.15582462684655,
    0.179167102876311,
    0.216598366309568,
    0.26603216591513,
    0.324083541154487,
    0.386820517224485,
    0.450113246364987,
    0.509323652377758,
    0.558573294272359,
    0.590463672842883,
    0.597264437175175,
    0.573745920439652,
    0.520380960986617,
    0.444758790690755,
    0.359750166225963,
    0.278987238175495,
    0.21206887381186,
    0.162091637731931,
    0.12651426193784,
    0.100279095709724,
    0.0790468365289463,
    0.0608671439409344,
    0.0459482418843583,
    0.0353311402777704,
    0.0295872989249962,
    0.0282323900120659,
    0.0298985530326881,
    0.0328961840601709,
    0.0357555698838089,
    0.0375239368848331
  )
)
df1
#>       x            y
#> 1     1 0.0004046080
#> 2     2 0.0004183862
#> 3     3 0.0004307944
#> 4     4 0.0004416088
#> 5     5 0.0004507201
#> 6     6 0.0004583539
#> 7     7 0.0004652443
#> 8     8 0.0004726054
#> 9     9 0.0004818365
#> 10   10 0.0004940313
#> 11   11 0.0005094779
#> 12   12 0.0005273869
#> 13   13 0.0005460538
#> 14   14 0.0005635363
#> 15   15 0.0005786947
#> 16   16 0.0005921849
#> 17   17 0.0006068804
#> 18   18 0.0006274043
#> 19   19 0.0006589518
#> 20   20 0.0007060223
#> 21   21 0.0007716956
#> 22   22 0.0008576657
#> 23   23 0.0009648217
#> 24   24 0.0010941098
#> 25   25 0.0012476202
#> 26   26 0.0014298502
#> 27   27 0.0016487114
#> 28   28 0.0019154953
#> 29   29 0.0022433336
#> 30   30 0.0026448413
#> 31   31 0.0031309702
#> 32   32 0.0037135743
#> 33   33 0.0044131165
#> 34   34 0.0052705176
#> 35   35 0.0063595103
#> 36   36 0.0077944212
#> 37   37 0.0097289185
#> 38   38 0.0123436051
#> 39   39 0.0158230846
#> 40   40 0.0203248591
#> 41   41 0.0259430774
#> 42   42 0.0326716119
#> 43   43 0.0403751638
#> 44   44 0.0487822072
#> 45   45 0.0575125639
#> 46   46 0.0661386624
#> 47   47 0.0742571414
#> 48   48 0.0815347852
#> 49   49 0.0877087304
#> 50   50 0.0925592583
#> 51   51 0.0958964121
#> 52   52 0.0975778468
#> 53   53 0.0975299736
#> 54   54 0.0957428778
#> 55   55 0.0922662141
#> 56   56 0.0872674196
#> 57   57 0.0811466237
#> 58   58 0.0745892159
#> 59   59 0.0684282888
#> 60   60 0.0633408656
#> 61   61 0.0595754290
#> 62   62 0.0569149855
#> 63   63 0.0549012076
#> 64   64 0.0531548543
#> 65   65 0.0515892280
#> 66   66 0.0504152092
#> 67   67 0.0499599636
#> 68   68 0.0504031217
#> 69   69 0.0515799826
#> 70   70 0.0529930989
#> 71   71 0.0540733795
#> 72   72 0.0545753160
#> 73   73 0.0548889929
#> 74   74 0.0560788146
#> 75   75 0.0595912908
#> 76   76 0.0667366977
#> 77   77 0.0781688004
#> 78   78 0.0935992679
#> 79   79 0.1118632110
#> 80   80 0.1312614685
#> 81   81 0.1499689436
#> 82   82 0.1663040890
#> 83   83 0.1787923975
#> 84   84 0.1861425194
#> 85   85 0.1873702550
#> 86   86 0.1822387919
#> 87   87 0.1719086977
#> 88   88 0.1593768223
#> 89   89 0.1492321798
#> 90   90 0.1466185219
#> 91   91 0.1558246268
#> 92   92 0.1791671029
#> 93   93 0.2165983663
#> 94   94 0.2660321659
#> 95   95 0.3240835412
#> 96   96 0.3868205172
#> 97   97 0.4501132464
#> 98   98 0.5093236524
#> 99   99 0.5585732943
#> 100 100 0.5904636728
#> 101 101 0.5972644372
#> 102 102 0.5737459204
#> 103 103 0.5203809610
#> 104 104 0.4447587907
#> 105 105 0.3597501662
#> 106 106 0.2789872382
#> 107 107 0.2120688738
#> 108 108 0.1620916377
#> 109 109 0.1265142619
#> 110 110 0.1002790957
#> 111 111 0.0790468365
#> 112 112 0.0608671439
#> 113 113 0.0459482419
#> 114 114 0.0353311403
#> 115 115 0.0295872989
#> 116 116 0.0282323900
#> 117 117 0.0298985530
#> 118 118 0.0328961841
#> 119 119 0.0357555699
#> 120 120 0.0375239369

Created on 2020-10-15 by the reprex package (v0.3.0.9001)

Thanks. Besides the data presentation here, my question is how to get the three peak values in y and the corresponding x values. I marked them in the figure below.Inkedrfigure2

Every problem in R can be approached with advantage as a problem in school algebra: f(x) = y, where the three objects are x, what's at hand, y, what is desired and f the function to transform the one into the other.

Here, y is a vector with the values of local maxima of a vector. As often is the case, f is a composed function.

> which(diff(sign(diff(df1$y)))==-2)+1
[1]  52  85 101

which can be deconstructed by examining piecewise the return values of each function from the inside out.

Thanks. Could I ask what is the theory for this code?
which(diff(sign(diff(df1$y)))==-2)+1

I tried for my data, but get the wrong results.
The figure is like this, but I get the x values as: 42 75 91, which are improper obviously.fig1

If there is just one maximum y value, I can use max(df1$y), and get the index of x. But there are several maximum y values. Thanks.

That data differs from df1

library(ggplot2)
df1 <- data.frame(
  x = 1:120,
  y = c(
    0.000404607994364647,
    0.000418386229955128,
    0.000430794394524755,
    0.000441608763817604,
    0.000450720069991295,
    0.000458353928139761,
    0.000465244320438098,
    0.000472605442408119,
    0.000481836517062748,
    0.000494031258256718,
    0.000509477897425229,
    0.000527386888743664,
    0.000546053806503485,
    0.000563536347150758,
    0.000578694693557497,
    0.000592184934692638,
    0.000606880432664763,
    0.00062740434643328,
    0.000658951780340787,
    0.000706022263900694,
    0.000771695560505854,
    0.000857665701641296,
    0.000964821690647073,
    0.00109410977624778,
    0.00124762020201182,
    0.00142985018853286,
    0.00164871137836403,
    0.00191549532233234,
    0.00224333357803463,
    0.00264484132738258,
    0.00313097018738546,
    0.00371357432026318,
    0.00441311650708302,
    0.00527051763162742,
    0.00635951029446201,
    0.00779442118237125,
    0.0097289184514785,
    0.0123436051467147,
    0.0158230846458223,
    0.0203248591277636,
    0.0259430773877464,
    0.0326716119103998,
    0.0403751637620159,
    0.0487822072022065,
    0.0575125639221519,
    0.0661386623990792,
    0.0742571414246157,
    0.0815347852470562,
    0.0877087303679977,
    0.0925592583028204,
    0.0958964121231122,
    0.097577846789136,
    0.0975299735518198,
    0.0957428778093581,
    0.0922662141274413,
    0.0872674196185096,
    0.0811466237435986,
    0.0745892159235215,
    0.0684282888234455,
    0.0633408656175527,
    0.0595754289702118,
    0.0569149855310436,
    0.0549012076098515,
    0.0531548543217289,
    0.0515892280330073,
    0.0504152091574836,
    0.0499599635705786,
    0.0504031217012216,
    0.0515799825745852,
    0.0529930988557304,
    0.0540733795338386,
    0.0545753160201401,
    0.0548889928851148,
    0.0560788146361983,
    0.0595912907883012,
    0.0667366977351586,
    0.0781688004268402,
    0.0935992678733076,
    0.111863211025667,
    0.131261468491735,
    0.14996894361554,
    0.16630408902095,
    0.178792397549176,
    0.186142519356196,
    0.187370254991816,
    0.182238791895045,
    0.171908697734763,
    0.159376822267072,
    0.149232179773896,
    0.14661852192979,
    0.15582462684655,
    0.179167102876311,
    0.216598366309568,
    0.26603216591513,
    0.324083541154487,
    0.386820517224485,
    0.450113246364987,
    0.509323652377758,
    0.558573294272359,
    0.590463672842883,
    0.597264437175175,
    0.573745920439652,
    0.520380960986617,
    0.444758790690755,
    0.359750166225963,
    0.278987238175495,
    0.21206887381186,
    0.162091637731931,
    0.12651426193784,
    0.100279095709724,
    0.0790468365289463,
    0.0608671439409344,
    0.0459482418843583,
    0.0353311402777704,
    0.0295872989249962,
    0.0282323900120659,
    0.0298985530326881,
    0.0328961840601709,
    0.0357555698838089,
    0.0375239368848331
  )
)


which(diff(sign(diff(df1$y)))==-2)+1 -> a

a
#> [1]  52  85 101

p <- ggplot(df1, aes(x,y)) + geom_point() + theme_minimal()

p + 
  geom_vline(xintercept = a[1]) +
  geom_vline(xintercept = a[2]) +
  geom_vline(xintercept = a[3])

Created on 2020-10-15 by the reprex package (v0.3.0.9001)

There is no theory to the code—it is a simple composed function that operates on a data frame to return the local maxima. To see how the function works, unravel it from the inside out.

which performs a logical test on some object and returns the index of those elements of the object stratifying the condition, in this case equality with 2.

The object that which tests is wrapped in diff, which wraps sign, which wraps diff operates on df$y`.

Working inside out,

df1$y is a vector of doubles in the closed interval [0.0004046,0.5972644].

Applying diff to it turns the following result:

If x is a vector of length n and differences = 1, then the computed result is equal to the successive differences x[(1+lag):n] - x[1:(n-lag)].
If difference is larger than one this algorithm is applied recursively to x. Note that the returned value is a vector which is shorter than x.

The application of diff thus returns a vector in the closed interval [-0.0850086,0.0632927].

Then

sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).

Running that through diff produces a vector in the closed interval [-3.032e-02,1.694e-02]

The which statement identifies the rows of df1 in which that vector equals -2.

The final operation adds 1 to those results.

Two ways to confirm this are to look at the plot intercepts on the x-axis to confirm that the values coincide with the local maxima or to inspect df1 around the corresponding row indices to determine if there are any competing candidates for local maxima.

Two choices remain—to treat the snippet as magical or to trace each step by hand until understood and then to construct a proof that the result is necessarily true for any arbitrary vector.

Thanks, I still could not figure this out. Yes, the data differs from df1 because it is long. I subset for part of my dataset. How to do it if x is a vector of length n and differences < 1?fig1

Is it possible to get the local maxima, if x values have unequal intervals? Thanks very much.

It is not possible to tell why a function returns a different result on one data frame than another. The plot in the OP featured local maxima at the points indicated in the answer. The plot here shows that in the range of those points that the plot xy is increasing monotonically over the same range.

If the function is not working to detect maxima in this different data frame, at approximately 100, 130, and 375 (by eye) nothing can be said without seeing the different data frame.

x values do not have to be equally spaced, because the function uses the sequential index. However, as presented the problem posed was one in which the x values were identical to their index. So, if there are not 375 values, the 375th index is never going to be reached.

I'll think about how to create an irregularly spaced x axis and revisit the problem.

I just made a simple example, now how to do with the real data? Thanks.

df1 = data.frame(x=c(16
16.564238781462
17.1483754005807
17.7531115530855
18.3791736799526
19.0273138400435
19.6983106135187
20.3929700371082
21.1121265723663
21.8566441080703
22.6274169979695
23.42537113513
24.2514650641664
25.106691132696
25.9920766833995
26.9086852881189
27.857618025476
28.8400148035466
29.8570557291778
30.9099625255951
32
33.1284775629241
34.2967508011614
35.506223106171
36.7583473599051
38.0546276800871
39.3966212270373
40.7859400742164
42.2242531447326
43.7132882161407
45.254833995939
46.85074227026
48.5029301283327
50.213382265392
51.9841533667991
53.8173705762377
55.7152360509519
57.6800296070931
59.7141114583557
61.8199250511901
64
66.2569551258482
68.5935016023228
71.0124462123421
73.5166947198102
76.1092553601741
78.7932424540747
81.5718801484328
84.4485062894653
87.4265764322813
90.5096679918781
93.70148454052
97.0058602566655
100.426764530784
103.968306733598
107.634741152475
111.430472101904
115.360059214186
119.428222916711
123.63985010238
128
132.513910251696
137.187003204646
142.024892424684
147.03338943962
152.218510720348
157.586484908149
163.143760296866
168.897012578931
174.853152864563
181.019335983756
187.40296908104
194.011720513331
200.853529061568
207.936613467196
215.269482304951
222.860944203808
230.720118428373
238.856445833423
247.279700204761
256
265.027820503393
274.374006409291
284.049784849368
294.066778879241
304.437021440697
315.172969816299
326.287520593731
337.794025157861
349.706305729125
362.038671967512
374.80593816208
388.023441026662
401.707058123136
415.873226934393
430.538964609902
445.721888407616
461.440236856745
477.712891666846
494.559400409521
512
530.055641006786
548.748012818582
568.099569698737
588.133557758482
608.874042881393
630.345939632597
652.575041187462
675.588050315722
699.412611458251),
y=c(0.000509477897425229
0.000527386888743664
0.000546053806503485
0.000563536347150758
0.000578694693557497
0.000592184934692638
0.000606880432664763
0.00062740434643328
0.000658951780340787
0.000706022263900694
0.000771695560505854
0.000857665701641295
0.000964821690647073
0.00109410977624778
0.00124762020201182
0.00142985018853286
0.00164871137836403
0.00191549532233234
0.00224333357803463
0.00264484132738257
0.00313097018738546
0.00371357432026318
0.004413116507083
0.00527051763162741
0.00635951029446201
0.00779442118237125
0.00972891845147851
0.0123436051467146
0.0158230846458223
0.0203248591277636
0.0259430773877464
0.0326716119103998
0.0403751637620158
0.0487822072022065
0.0575125639221519
0.0661386623990792
0.0742571414246156
0.0815347852470562
0.0877087303679976
0.0925592583028204
0.0958964121231122
0.0975778467891361
0.0975299735518198
0.0957428778093582
0.0922662141274413
0.0872674196185096
0.0811466237435986
0.0745892159235216
0.0684282888234455
0.0633408656175527
0.0595754289702118
0.0569149855310437
0.0549012076098515
0.0531548543217289
0.0515892280330073
0.0504152091574836
0.0499599635705786
0.0504031217012216
0.0515799825745852
0.0529930988557304
0.0540733795338386
0.0545753160201401
0.0548889928851148
0.0560788146361983
0.0595912907883012
0.0667366977351586
0.0781688004268401
0.0935992678733074
0.111863211025667
0.131261468491735
0.14996894361554
0.166304089020949
0.178792397549176
0.186142519356196
0.187370254991816
0.182238791895045
0.171908697734763
0.159376822267072
0.149232179773896
0.14661852192979
0.15582462684655
0.17916710287631
0.216598366309568
0.26603216591513
0.324083541154487
0.386820517224485
0.450113246364986
0.509323652377758
0.558573294272359
0.590463672842883
0.597264437175175
0.573745920439653
0.520380960986617
0.444758790690755
0.359750166225963
0.278987238175495
0.21206887381186
0.162091637731931
0.12651426193784
0.100279095709724
0.0790468365289463
0.0608671439409344
0.0459482418843581
0.0353311402777704
0.0295872989249962
0.0282323900120659
0.0298985530326881
0.032896184060171
0.0357555698838089
0.0375239368848331))

I pasted the real data here. Thanks for your help.

1 Like

The c function can take multiple arguments, but they must be separate by a ,

df1 <- data.frame(
  x = c(
    16,
    16.564238781462,
    17.1483754005807,
    17.7531115530855,
    18.3791736799526,
    19.0273138400435,
    19.6983106135187,
    20.3929700371082,
    21.1121265723663,
    21.8566441080703,
    22.6274169979695,
    23.42537113513,
    24.2514650641664,
    25.106691132696,
    25.9920766833995,
    26.9086852881189,
    27.857618025476,
    28.8400148035466,
    29.8570557291778,
    30.9099625255951,
    32,
    33.1284775629241,
    34.2967508011614,
    35.506223106171,
    36.7583473599051,
    38.0546276800871,
    39.3966212270373,
    40.7859400742164,
    42.2242531447326,
    43.7132882161407,
    45.254833995939,
    46.85074227026,
    48.5029301283327,
    50.213382265392,
    51.9841533667991,
    53.8173705762377,
    55.7152360509519,
    57.6800296070931,
    59.7141114583557,
    61.8199250511901,
    64,
    66.2569551258482,
    68.5935016023228,
    71.0124462123421,
    73.5166947198102,
    76.1092553601741,
    78.7932424540747,
    81.5718801484328,
    84.4485062894653,
    87.4265764322813,
    90.5096679918781,
    93.70148454052,
    97.0058602566655,
    100.426764530784,
    103.968306733598,
    107.634741152475,
    111.430472101904,
    115.360059214186,
    119.428222916711,
    123.63985010238,
    128,
    132.513910251696,
    137.187003204646,
    142.024892424684,
    147.03338943962,
    152.218510720348,
    157.586484908149,
    163.143760296866,
    168.897012578931,
    174.853152864563,
    181.019335983756,
    187.40296908104,
    194.011720513331,
    200.853529061568,
    207.936613467196,
    215.269482304951,
    222.860944203808,
    230.720118428373,
    238.856445833423,
    247.279700204761,
    256,
    265.027820503393,
    274.374006409291,
    284.049784849368,
    294.066778879241,
    304.437021440697,
    315.172969816299,
    326.287520593731,
    337.794025157861,
    349.706305729125,
    362.038671967512,
    374.80593816208,
    388.023441026662,
    401.707058123136,
    415.873226934393,
    430.538964609902,
    445.721888407616,
    461.440236856745,
    477.712891666846,
    494.559400409521,
    512,
    530.055641006786,
    548.748012818582,
    568.099569698737,
    588.133557758482,
    608.874042881393,
    630.345939632597,
    652.575041187462,
    675.588050315722,
    699.412611458251
  ),
  y = c(
    0.000509477897425229,
    0.000527386888743664,
    0.000546053806503485,
    0.000563536347150758,
    0.000578694693557497,
    0.000592184934692638,
    0.000606880432664763,
    0.00062740434643328,
    0.000658951780340787,
    0.000706022263900694,
    0.000771695560505854,
    0.000857665701641295,
    0.000964821690647073,
    0.00109410977624778,
    0.00124762020201182,
    0.00142985018853286,
    0.00164871137836403,
    0.00191549532233234,
    0.00224333357803463,
    0.00264484132738257,
    0.00313097018738546,
    0.00371357432026318,
    0.004413116507083,
    0.00527051763162741,
    0.00635951029446201,
    0.00779442118237125,
    0.00972891845147851,
    0.0123436051467146,
    0.0158230846458223,
    0.0203248591277636,
    0.0259430773877464,
    0.0326716119103998,
    0.0403751637620158,
    0.0487822072022065,
    0.0575125639221519,
    0.0661386623990792,
    0.0742571414246156,
    0.0815347852470562,
    0.0877087303679976,
    0.0925592583028204,
    0.0958964121231122,
    0.0975778467891361,
    0.0975299735518198,
    0.0957428778093582,
    0.0922662141274413,
    0.0872674196185096,
    0.0811466237435986,
    0.0745892159235216,
    0.0684282888234455,
    0.0633408656175527,
    0.0595754289702118,
    0.0569149855310437,
    0.0549012076098515,
    0.0531548543217289,
    0.0515892280330073,
    0.0504152091574836,
    0.0499599635705786,
    0.0504031217012216,
    0.0515799825745852,
    0.0529930988557304,
    0.0540733795338386,
    0.0545753160201401,
    0.0548889928851148,
    0.0560788146361983,
    0.0595912907883012,
    0.0667366977351586,
    0.0781688004268401,
    0.0935992678733074,
    0.111863211025667,
    0.131261468491735,
    0.14996894361554,
    0.166304089020949,
    0.178792397549176,
    0.186142519356196,
    0.187370254991816,
    0.182238791895045,
    0.171908697734763,
    0.159376822267072,
    0.149232179773896,
    0.14661852192979,
    0.15582462684655,
    0.17916710287631,
    0.216598366309568,
    0.26603216591513,
    0.324083541154487,
    0.386820517224485,
    0.450113246364986,
    0.509323652377758,
    0.558573294272359,
    0.590463672842883,
    0.597264437175175,
    0.573745920439653,
    0.520380960986617,
    0.444758790690755,
    0.359750166225963,
    0.278987238175495,
    0.21206887381186,
    0.162091637731931,
    0.12651426193784,
    0.100279095709724,
    0.0790468365289463,
    0.0608671439409344,
    0.0459482418843581,
    0.0353311402777704,
    0.0295872989249962,
    0.0282323900120659,
    0.0298985530326881,
    0.032896184060171,
    0.0357555698838089,
    0.0375239368848331
  )
)

Created on 2020-10-15 by the reprex package (v0.3.0.9001)

suppressPackageStartupMessages({
  library(ggplot2)
})
df1 <- data.frame(
  x = c(
    16,
    16.564238781462,
    17.1483754005807,
    17.7531115530855,
    18.3791736799526,
    19.0273138400435,
    19.6983106135187,
    20.3929700371082,
    21.1121265723663,
    21.8566441080703,
    22.6274169979695,
    23.42537113513,
    24.2514650641664,
    25.106691132696,
    25.9920766833995,
    26.9086852881189,
    27.857618025476,
    28.8400148035466,
    29.8570557291778,
    30.9099625255951,
    32,
    33.1284775629241,
    34.2967508011614,
    35.506223106171,
    36.7583473599051,
    38.0546276800871,
    39.3966212270373,
    40.7859400742164,
    42.2242531447326,
    43.7132882161407,
    45.254833995939,
    46.85074227026,
    48.5029301283327,
    50.213382265392,
    51.9841533667991,
    53.8173705762377,
    55.7152360509519,
    57.6800296070931,
    59.7141114583557,
    61.8199250511901,
    64,
    66.2569551258482,
    68.5935016023228,
    71.0124462123421,
    73.5166947198102,
    76.1092553601741,
    78.7932424540747,
    81.5718801484328,
    84.4485062894653,
    87.4265764322813,
    90.5096679918781,
    93.70148454052,
    97.0058602566655,
    100.426764530784,
    103.968306733598,
    107.634741152475,
    111.430472101904,
    115.360059214186,
    119.428222916711,
    123.63985010238,
    128,
    132.513910251696,
    137.187003204646,
    142.024892424684,
    147.03338943962,
    152.218510720348,
    157.586484908149,
    163.143760296866,
    168.897012578931,
    174.853152864563,
    181.019335983756,
    187.40296908104,
    194.011720513331,
    200.853529061568,
    207.936613467196,
    215.269482304951,
    222.860944203808,
    230.720118428373,
    238.856445833423,
    247.279700204761,
    256,
    265.027820503393,
    274.374006409291,
    284.049784849368,
    294.066778879241,
    304.437021440697,
    315.172969816299,
    326.287520593731,
    337.794025157861,
    349.706305729125,
    362.038671967512,
    374.80593816208,
    388.023441026662,
    401.707058123136,
    415.873226934393,
    430.538964609902,
    445.721888407616,
    461.440236856745,
    477.712891666846,
    494.559400409521,
    512,
    530.055641006786,
    548.748012818582,
    568.099569698737,
    588.133557758482,
    608.874042881393,
    630.345939632597,
    652.575041187462,
    675.588050315722,
    699.412611458251
  ),
  y = c(
    0.000509477897425229,
    0.000527386888743664,
    0.000546053806503485,
    0.000563536347150758,
    0.000578694693557497,
    0.000592184934692638,
    0.000606880432664763,
    0.00062740434643328,
    0.000658951780340787,
    0.000706022263900694,
    0.000771695560505854,
    0.000857665701641295,
    0.000964821690647073,
    0.00109410977624778,
    0.00124762020201182,
    0.00142985018853286,
    0.00164871137836403,
    0.00191549532233234,
    0.00224333357803463,
    0.00264484132738257,
    0.00313097018738546,
    0.00371357432026318,
    0.004413116507083,
    0.00527051763162741,
    0.00635951029446201,
    0.00779442118237125,
    0.00972891845147851,
    0.0123436051467146,
    0.0158230846458223,
    0.0203248591277636,
    0.0259430773877464,
    0.0326716119103998,
    0.0403751637620158,
    0.0487822072022065,
    0.0575125639221519,
    0.0661386623990792,
    0.0742571414246156,
    0.0815347852470562,
    0.0877087303679976,
    0.0925592583028204,
    0.0958964121231122,
    0.0975778467891361,
    0.0975299735518198,
    0.0957428778093582,
    0.0922662141274413,
    0.0872674196185096,
    0.0811466237435986,
    0.0745892159235216,
    0.0684282888234455,
    0.0633408656175527,
    0.0595754289702118,
    0.0569149855310437,
    0.0549012076098515,
    0.0531548543217289,
    0.0515892280330073,
    0.0504152091574836,
    0.0499599635705786,
    0.0504031217012216,
    0.0515799825745852,
    0.0529930988557304,
    0.0540733795338386,
    0.0545753160201401,
    0.0548889928851148,
    0.0560788146361983,
    0.0595912907883012,
    0.0667366977351586,
    0.0781688004268401,
    0.0935992678733074,
    0.111863211025667,
    0.131261468491735,
    0.14996894361554,
    0.166304089020949,
    0.178792397549176,
    0.186142519356196,
    0.187370254991816,
    0.182238791895045,
    0.171908697734763,
    0.159376822267072,
    0.149232179773896,
    0.14661852192979,
    0.15582462684655,
    0.17916710287631,
    0.216598366309568,
    0.26603216591513,
    0.324083541154487,
    0.386820517224485,
    0.450113246364986,
    0.509323652377758,
    0.558573294272359,
    0.590463672842883,
    0.597264437175175,
    0.573745920439653,
    0.520380960986617,
    0.444758790690755,
    0.359750166225963,
    0.278987238175495,
    0.21206887381186,
    0.162091637731931,
    0.12651426193784,
    0.100279095709724,
    0.0790468365289463,
    0.0608671439409344,
    0.0459482418843581,
    0.0353311402777704,
    0.0295872989249962,
    0.0282323900120659,
    0.0298985530326881,
    0.032896184060171,
    0.0357555698838089,
    0.0375239368848331
  )
)

p <- ggplot(df1, aes(x, y)) +
  geom_point() +
  theme_minimal()
p

which(diff(sign(diff(df1$y))) == -2) + 1 -> positions
positions
#> [1] 42 75 91
indices <- df1[positions, 1]
p +
  geom_vline(xintercept = indices[1]) +
  geom_vline(xintercept = indices[2]) +
  geom_vline(xintercept = indices[3])

Created on 2020-10-16 by the reprex package (v0.3.0.9001)

1 Like

Thanks, it works, but how to find the corresponding y values? For example, the first element could be found, but the second and third could not be found.
df1[indices[1],] works, but

df1[indices[2],] and df1[indices[3],] do not work. Thanks very much.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I pasted my original data in the post, though I didn't know how to format it properly. Thanks for your suggestion though.

The reason I shared that with you, in light of the fact that you don't know how to format your data properly , is that it makes it much harder for other forum users to take your data and therefore to understand your issue, and help you.

Therefore , if you don't take the time to read the guide and learn how to format it properly, you make it harder for others to help you... but it is completely your choice to do that or not to that.

Do you mean the value of y at the value of each x local maximum?

which(diff(sign(diff(df1$y))) == -2) + 1 -> positions
positions
#> [1] 42 75 91
indices <- df1[positions, 1]

df1[indices[1],]
df1[indices[2],]
df1[indices[3],]

works fine for me...