Search code examples
rheartrate

How to eliminate variables when they are to close together (i.e. closer than 200 ms -> delete the row )


first post today.

Currently i am working on my research and i have to analyze heart rate data. Now that i've completed most of my code i came to the discovery that the algorithm i am working in (RHRV) utilizes the occurences of the heartbeats, with matching seconds.

My simple txt file, in seconds, therefore looks like this (1 col, 433 rows): 0.905 1.943 2.941 4.005 4.998 5.921

These are the heart beats and the second they occur.

My question now is: how do i delete the occurrences that are impossible like: 2 occurences/heartbeats happening faster that 0.2 seconds (200 ms)

Is there an easy way or function that allows me to alter the data?

Looking forward to your answers!

Cheers,

Sander


Solution

  • I couldn't think of a vectorised way to do this but this could work. Note that this will be expensive for long vectors as the expression y <- c(y, x[i]) will keep reconstructing the vector y.

    x <- c(171.1156, 171.1345, 171.256, 171.309, 171.465, 171.4986, 171.986, 172.563)
    m <- 1
    y <- numeric(0)
    y[m] <- x[1]
    for (i in 2:NROW(x)) {
        if (y[m] + 0.2 < x[i]) {
            m <- m + 1
            y <- c(y, x[i])
        }
    }
    y
    # output
    [1] 171.1156 171.4650 171.9860 172.5630
    

    I iterate through the elements of x and every time I encounter a value that is more than 0.2 away from the latest accepted value in y, I update y with that x[i].