Detect peaks with two adjacent identical values using pracma::findpeaks

Viewed 107

I've got some data with 23 peaks. I've used pracma::findpeaks to pick out the peaks. However, one of the peaks has two identical values adjacent each other, at time=7524 and time=7525. It seems findpeaks deals with this by ignoring the peak.

Could I please ask if someone could help me make it recognise it. I'd like it to pick out the first of the two peaks, though it would also be good to know how to make it pick out the last of them as well

data <- data.frame(time=c(1562, 1563, 1564, 1565, 1566, 1810, 1811, 1812, 1813, 1814, 
                          2058, 2059, 2060, 2061, 2306, 2307, 2308, 2309, 2310, 2560, 2561, 
                          2562, 2563, 2564, 3064, 3065, 3066, 3067, 3580, 3581, 3582, 3583, 
                          3584, 4095, 4096, 4097, 4098, 4099, 4610, 4611, 4612, 4613, 4614, 
                          5128, 5129, 5130, 5131, 5132, 5133, 5637, 5638, 5639, 5640, 5641, 
                          5876, 5877, 5878, 5879, 5880, 5881, 5882, 6125, 6126, 6127, 6128, 
                          6129, 6130, 6607, 6608, 6609, 6610, 6611, 6612, 6613, 7072, 7073, 
                          7074, 7075, 7076, 7077, 7078, 7079, 7519, 7520, 7521, 7522, 7523, 
                          7524, 7525, 7526, 7527, 7528, 7941, 7942, 7943, 7944, 7945, 7946, 
                          7947, 7948, 7949, 8342, 8343, 8344, 8345, 8346, 8347, 8348, 8349, 
                          8350, 8351, 8708, 8709, 8710, 8711, 8712, 8713, 8714, 8715, 8716, 
                          8717, 8718, 9045, 9046, 9047, 9048, 9049, 9050, 9051, 9052, 9053, 
                          9054, 9055, 9352, 9353, 9354, 9355, 9356, 9357, 9358, 9359, 9360, 
                          9361, 9362, 9363, 9624, 9625, 9626, 9627, 9628, 9629, 9630, 9631, 
                          9632, 9633, 9634, 9867, 9868, 9869, 9870, 9871, 9872, 9873, 9874, 
                          9875, 9876),
                   value=c(509, 672, 758, 686, 584, 559, 727, 759, 688, 528, 562, 711, 
                          768, 678, 644, 750, 822, 693, 531, 566, 738, 793, 730, 511, 587, 
                          739, 761, 651, 579, 747, 768, 705, 544, 551, 687, 756, 749, 645, 
                          564, 680, 724, 691, 596, 535, 625, 685, 689, 612, 512, 537, 616, 
                          657, 653, 573, 506, 598, 675, 685, 668, 609, 515, 575, 656, 687, 
                          678, 626, 533, 509, 587, 641, 680, 663, 602, 515, 505, 583, 646, 
                          693, 696, 684, 630, 549, 500, 572, 637, 681, 725, 736, 736, 703, 
                          649, 556, 568, 637, 682, 743, 765, 767, 709, 660, 587, 548, 622, 
                          690, 761, 779, 764, 749, 694, 631, 525, 571, 646, 724, 788, 811, 
                          834, 818, 776, 712, 616, 536, 556, 649, 738, 801, 857, 866, 837, 
                          808, 718, 647, 568, 508, 605, 714, 823, 872, 917, 916, 890, 825, 
                          742, 642, 543, 549, 656, 766, 851, 921, 947, 951, 892, 830, 730, 
                          617, 586, 675, 760, 804, 816, 795, 740, 690, 613, 522))


peaks <- data.frame(findpeaks(data$value, npeaks=23, threshold=100, sortstr=TRUE))
data$n <- seq(1,length(data$value))
data <- merge(x=data, y=peaks, by.x="n", by.y="X2", all.x=TRUE, all.y=TRUE)

ggplot(data, aes(x=time, y=value)) +
  geom_col(fill="red") +
  geom_point(aes(x=time, y=X1))

enter image description here

0 Answers
Related