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

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...

It still does not work for me. I don't why there is the difference

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

You probably have NAs in your data? (If so remove them).

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
  )
)
which(diff(sign(diff(df1$y))) == -2) + 1 -> positions
positions
#> [1] 42 75 91
indices <- df1[positions, 1]
indices
#> [1]  66.25696 207.93661 362.03867
df1[positions[1],]
#>           x          y
#> 42 66.25696 0.09757785
df1[positions[2],]
#>           x         y
#> 75 207.9366 0.1873703
df1[positions[3],]
#>           x         y
#> 91 362.0387 0.5972644

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

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.