# Finding Peaks for Heart Rate data

I am trying to detect the max peaks in a set of data that measures systole and diastole of a heart beat. I am hoping I can use R to find the number of peaks within a certain time range to find the heart rate. This data is over a period of 2.5 seconds(rows) and I need the heart rate for each data point(columns). Right now, I am using ggplot to graph this data then counting the peaks manually and manually typing in that to the equation in the code ((60/2.5)* # of peaks). Any help would be awesome. Thanks!
I cant figure out to put the sample csv file on here though either. If that is needed let me know.

Hi,

Welcome to the RStudio community!

I think I understand the question, and your description of the data seems straightforward, but I suggest you look at the guide below on how to create a reprex. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Even if you don't have code yet you can look at the part on how to create a small dataset you can share here.

I'll try and look into it meanwhile,

Hope this helps,
PJ

Thank you! Here are two columns of example data. The b1 column is the pressure measurement and the time.s column is the time the measurement was taken. I chose to do 100 rows just so that multiple peaks would be present in the data(which I need to be accurately counted using some function). If you need anything else let me know. I am very new to R so it may take me a little time to get things running but I am making progress. Thanks!

data.frame(b1 = c(14.736410628656, 14.7422931355985, 14.9986455061322,
15.1676821497013, 14.8836253074118, 14.7570927793282, 14.9772433937102,
15.8952789184474, 18.7419948081792, 24.4468789441611, 32.10039189247,
40.0741893600385, 47.3302263863307, 52.5844987976863, 55.8212046572726,
56.9570850868269, 56.5770694147446, 55.0840241276885, 52.2521477879902,
49.575147284815, 46.3781068268194, 42.2533543718202, 38.0849564107362,
33.4851355209457, 29.1442262455308, 25.4674584603228, 22.4287661567552,
20.0036906354311, 17.9959959188032, 16.7131048711442, 16.0205651314969,
15.3774585247997, 14.7646386376271, 14.6520609021805, 14.8509432154032,
14.8289723910906, 14.7657489508918, 14.8424658369896, 14.8303150759215,
14.6511878158236, 14.6700297471029, 15.5437171181136, 18.0829987563313,
23.2553042585183, 30.7549433408655, 38.972279890321, 46.28503444154,
51.2825685273259, 54.7219314093178, 56.6166245878517, 56.3546508376649,
54.9000838001672, 52.5671523220477, 49.7634815095047, 46.2244000815004,
42.1157054350087, 37.9828803333518, 33.5108318222804, 29.3688533094341,
25.8988039790237, 22.7701964465657, 20.2831297774573, 18.0279694241349,
16.8223892401827, 16.0413723464035, 15.2026402735054, 15.1163144735196,
15.1014000183537, 14.8274200887847, 14.3753041896144, 14.4278542341527,
14.8102217465772, 14.6829574052298, 14.5887656220083, 15.0801361128259,
15.1643043451417, 16.1419935222045, 19.8273457304614, 26.0283195655414,
34.0606536161329, 42.4127877017186, 49.0848049677252, 53.9741831614422,
56.8970499969471, 57.5871274744693, 56.9060874058915, 55.05097088088,
52.5527831986971, 49.4322409084902, 45.6098305603465, 41.4411913826586,
37.1368088193409, 32.6633452454046, 28.464416642104, 24.7746903804738,
21.9000441809223, 19.7920317403031, 17.8389386025885, 16.4425800793382,
15.7775408621563), time.s = c(0.005, 0.01, 0.015, 0.02, 0.025,
0.03, 0.035, 0.04, 0.045, 0.05, 0.055, 0.06, 0.065, 0.07,
0.075, 0.08, 0.085, 0.09, 0.095, 0.1, 0.105, 0.11, 0.115,
0.12, 0.125, 0.13, 0.135, 0.14, 0.145, 0.15, 0.155, 0.16,
0.165, 0.17, 0.175, 0.18, 0.185, 0.19, 0.195, 0.2, 0.205,
0.21, 0.215, 0.22, 0.225, 0.23, 0.235, 0.24, 0.245, 0.25,
0.255, 0.26, 0.265, 0.27, 0.275, 0.28, 0.285, 0.29, 0.295,
0.3, 0.305, 0.31, 0.315, 0.32, 0.325, 0.33, 0.335, 0.34,
0.345, 0.35, 0.355, 0.36, 0.365, 0.37, 0.375, 0.38, 0.385,
0.39, 0.395, 0.4, 0.405, 0.41, 0.415, 0.42, 0.425, 0.43,
0.435, 0.44, 0.445, 0.45, 0.455, 0.46, 0.465, 0.47, 0.475,
0.48, 0.485, 0.49, 0.495, 0.5))

I posted below, not sure if it replied to your post or not.

There are probably several ways to do this. One way is to use `findPeaks` from the `quantmod` package. This will find the index of the value at the peak. However, it doesn't care if the peak is large or small, so it maybe necessary to threshold the data. This would still fail if you had double peaks - so it would be worth checking that the function was reliable by plotting the data and overlaying the reported peaks.

``````peaks <- quantmod::findPeaks(df\$b1)
df %>%
slice(peaks) %>%
filter(b1 > 40) # threshold

``````

Thanks! I tried this out and it worked great. Gave me three peaks which I then counted after I graphed the data and it matched up. However, the threshold makes it difficult for me to put trust in it since peaks get lower than 40 later in the experiment. I lowered the threshold to 10 to see if this would work, but it resulted in four more peaks than were actually there.

This topic was automatically closed 21 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.