Search code examples
rinputloadingheartrate

How to load raw heart rate vector into RHRV data structure


I am struggling with the RHRV library in R. The library is made for heart rate variability analysis and supports data files formats like ASCII, EDF, Polar, Suunto and WFDB. RHRV uses a custom data structure called HRVData to store all HRV information related to the signal being analysed. However I am working with already loaded (from .csv file) RR vectors. RRs are distances between consecutive heartbeats (in milliseconds). My exemplary data looks like:

RR_series <- rnorm(300, mean = 1000, sd = 15)

My problem is to insert RR_series into any of RHRV function so I can conduct further analysis - I want to input RR_series into HRVData structure.

From RHRV Quick Start Tutorial I know that "RHRV imports data files containing heart beats positions" so in worst case I can obtain time position of the heartbeat peaks - but still those will be vectors that I need to transform into HRVData somehow.


Solution

  • The package uses a somewhat unusual mechanism for loading vectors. You need to create an empty HRVData object first using CreateHRVData(), then use this object as a base from which to add data. The package's functions seem designed to work best with a piped workflow.

    Note that you need to supply your data as seconds since recording started, which means instead of passing the vector of RR intervals, you should pass the cumsum of the RR intervals divided by 1000:

    RR_series <- rnorm(300, mean = 1000, sd = 15)
    
    RR_series <- cumsum(RR_series / 1000)
    

    Getting this into the correct format is now straightforward:

    library(RHRV)
    
    my_data <- CreateHRVData() |>
               LoadBeatVector(RR_series) |>
               BuildNIHR() |>
               InterpolateNIHR()
    

    For example, can see a plot of instantaneous heart rate over time like this:

    PlotHR(my_data)
    

    Created on 2022-06-10 by the reprex package (v2.0.1)