rdifftime

To calculate time differences, is there a way to change all : in a data frame?


I was wondering if there is a way to make all colons in a data frame a period? Or delete them from a data set all together?

I have asked people what time they went to sleep and they've put 21:00 for example. But now when it comes to minusing my data for finding how long they were asleep for, i dont think r likes the colon. Is there anyway to solve this and eliminate all :'s from the data set?

Any help would be greatly appreciated Thank you

I have thought about using the code:

data %>% dplyr::mutate(., QS1 = dplyr::recode(QS1, "01:00" = "0100"))

but it would take me ages to individually go through and do this to over 300 pieces of data


Solution

  • You probably have data like this

    df
    #   ID   QS1   QS2            X
    # 1  1 21:00 06:00 -0.008238534
    # 2  2 22:00 07:30  0.862295486
    # 3  3 21:30 07:00  0.451935629
    

    We only have time. Since somebody could got earlier to bed and woke up the same day we might run into some problems.

    Anyway, you can paste a date before your times, usually two consecutive days. Actually, it is not absolutely necessary to use the correct day, the main thing is that it is two consecutive days. Check if there are "unusually" sleepers as mentioned before.

    tcols <- c('QS1', 'QS2')
    df[tcols] <- Map(paste, c('2023-03-18', '2023-03-19'), df[tcols])
    

    Then all you need is difftime which recognizes time format automatically.

    df$dif <- difftime(df$QS2, df$QS1, units='hours')
    df
    #   ID              QS1              QS2          X       dif
    # 1  1 2023-03-18 21:00 2023-03-19 06:00  0.8199990 9.0 hours
    # 2  2 2023-03-18 22:00 2023-03-19 07:30 -0.8152736 9.5 hours
    # 3  3 2023-03-18 21:30 2023-03-19 07:00  1.1706443 9.5 hours
    

    where

    str(df)
    # 'data.frame': 3 obs. of  5 variables:
    # $ ID : int  1 2 3
    # $ QS1: chr  "2023-03-18 21:00" "2023-03-18 22:00" "2023-03-18 21:30"
    # $ QS2: chr  "2023-03-19 06:00" "2023-03-19 07:30" "2023-03-19 07:00"
    # $ X  : num  0.982 -0.252 -0.76
    # $ dif: 'difftime' num  9 9.5 9.5
    #  ..- attr(*, "units")= chr "hours"
    

    Note: To convert character time (with date already included) to "POSIXct" time format read this posts: [1] [2]


    Data:

    df <- structure(list(ID = 1:3, QS1 = c("21:00", "22:00", "21:30"), 
        QS2 = c("06:00", "07:30", "07:00"), X = c(-0.0616202790380149, 
        -1.5162238145615, -0.413276568120249)), class = "data.frame", row.names = c(NA, 
    -3L))