Search code examples
r

R Analysing data with different time stamps


I have a huge dataset of several laboratory results for several patients. In the example dataset I have several Puls measurements at different examinations. Puls1 was determined at baseline visit and Puls 2 at the second visit, etc. What I already did was to evaluate the changes over time by using ggplot, however, due to the huge amount of data, the plots are inappropriate. What I want to do is to detect those patients who had changes +/- 5 regarding their Puls looking at all of the different examinations (in this example Puls1, Puls2, Puls3).

Here is a small example dataset:

ID <- c(1, 2, 3)
Puls1 <- c(55, 70, 65)
Puls2 <- c(55, 70, 80)
Puls3 <- c(80, 70, 75)

So in this case of changes between +/-5 I would detect patient 1 and patient 3. Could anyone help me out here?


Solution

  • You can solve this problem by computing the differences between consecutive pulse measurements for each patient and checking if any difference exceeds +/-5:

    # Sample dataset
    ID <- c(1, 2, 3)
    Puls1 <- c(55, 70, 65)
    Puls2 <- c(55, 70, 80)
    Puls3 <- c(80, 70, 75)
    
    # Create a dataframe
    df <- data.frame(ID, Puls1, Puls2, Puls3)
    
    # Detect patients with changes greater than ±5
    df$flag <- apply(df[, -1], 1, function(x) any(diff(x) > 5 | diff(x) < -5))
    
    # Filter patients who have significant pulse changes
    patients_with_changes <- df[df$flag, "ID"]
    
    # Output result
    print(patients_with_changes)