1

I have a dataframe with two columns - "Time" and "value" having discontinuous data in "Time". How do I pick the top 10 maxima in "value" with at least a difference in "Time" of 3 units? I tried to use findPeaks function after converting the dataframe into a vector, but it doesn't seem to help. This is the code I tried:

Fulldata <- data.frame(x)

y <- subset(Fulldata, c(Time > 181.4 & Time < 181.69 | Time > 251 & Time < 251.84 | Time > 264.83 & Time < 265.28 ...... .... Time > 778.75 & Time < 779.45 | Time > 851.45 & Time < 852.22 ))

yvector <- as.vector(y$value)

peakmatrix <- findpeaks(yvector, minpeakdistance = 300, sortstr = TRUE)    
peakmatrix

dput(head(y, 50)) structure(list(Time = c(181.41, 181.42, 181.43, 181.44, 181.45, 181.46, 181.47, 181.48, 181.49, 181.5, 181.51, 181.52, 181.53, 181.54, 181.55, 181.56, 181.57, 181.58, 181.59, 181.6, 181.61, 181.62, 181.63, 181.64, 181.65, 181.66, 181.67, 181.68, 251.01, 251.02, 251.03, 251.04, 251.05, 251.06, 251.07, 251.08, 251.09, 251.1, 251.11, 251.12, 251.13, 251.14, 251.15, 251.16, 251.17, 251.18, 251.19, 251.2, 251.21, 251.22), value = c(0.0391672, 0.0426035, 0.0428215, 0.0420676, 0.0471682, 0.0503162, 0.045388, 0.0356645, 0.0302182, 0.0287721, 0.0254405, 0.0237114, 0.0221648, 0.0237992, 0.0250306, 0.0262008, 0.0260725, 0.0227329, 0.0228227, 0.0218701, 0.021884, 0.02352, 0.0256086, 0.0258213, 0.026324, 0.0271051, 0.0280477, 0.0269339, 0.017347, 0.0176336, 0.0187158, 0.0192151, 0.0194258, 0.0168017, 0.0152756, 0.0152147, 0.0157609, 0.0170017, 0.0170792, 0.0193993, 0.0259839, 0.0312447, 0.0356666, 0.0385061, 0.0423352, 0.0460978, 0.0476292, 0.0462294, 0.0439847, 0.0412032)), .Names = c("Time", "value"), row.names = c(18142L, 18143L, 18144L, 18145L, 18146L, 18147L, 18148L, 18149L, 18150L, 18151L, 18152L, 18153L, 18154L, 18155L, 18156L, 18157L, 18158L, 18159L, 18160L, 18161L, 18162L, 18163L, 18164L, 18165L, 18166L, 18167L, 18168L, 18169L, 25102L, 25103L, 25104L, 25105L, 25106L, 25107L, 25108L, 25109L, 25110L, 25111L, 25112L, 25113L, 25114L, 25115L, 25116L, 25117L, 25118L, 25119L, 25120L, 25121L, 25122L, 25123L), class = "data.frame")

Samar
  • 41
  • 4
  • 2
    Can you share some sample data? Preferably in a copy/pasteable way (either share code to simulate data or use `dput()` on an illustrative subset of your data). – Gregor Thomas May 08 '18 at 19:27
  • Since we don't have `Data.csv`, we can't run any of that code. Could you either share code to simulate data or use `dput()` on an illustrative subset of your data? If you're data's not too long, just put `dput(yvector)` in your console and paste the result into your question. If your data is very long then use `dput(head(yvector, 100))` to share the first 100 values. – Gregor Thomas May 08 '18 at 19:58
  • 1
    If you need more help sharing reproducible data, [this question has several good answers that give a nice guide to making reproducible examples in R](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas May 08 '18 at 20:01
  • I and many others don't like downloading data from random sites. It's much easier if you make things copy/pasteable for us. It really is quite easy with `dput()` if you try it. – Gregor Thomas May 08 '18 at 20:25
  • Hi Gregor, I am sorry for the inconvenience. I would not want to analyze the peaks from the vector form, since findPeaks seem to consider only the difference between the indices. So, I have added the subset of data of "y" in the code above. Hope that is fine! – Samar May 08 '18 at 20:46
  • Hmm, now that I can see your data the problem is clear. I think the `minpeakdistance = 300` doesn't work due to the gaps in your series. It might work to fill in the gaps with a constant (or maybe `NA`) so there are no intermediate peaks, and then run the command again. – Gregor Thomas May 09 '18 at 02:28
  • Hi Gregor, thank you for the suggestion. Will try that. – Samar May 09 '18 at 09:43
  • Hi Gregor, I have added NA values in the vector and the findpeaks function throws an error: "Error in xp[i] <- which.max(x[x1[i]:x2[i]]) + x1[i] - 1 : replacement has length zero". I am unable to see whats going wrong.. – Samar May 09 '18 at 13:39
  • If `NA` values don't work, try some constant that's lower than your values, like 0. – Gregor Thomas May 09 '18 at 14:12
  • 1
    Thanks a lot! that worked – Samar May 09 '18 at 14:22

0 Answers0