Calculating time duration from two time values with no date in R

て烟熏妆下的殇ゞ 提交于 2021-02-05 06:50:19

问题


I am trying to calculate sleep duration, in hours, from two times self-reported in a diary. I have the bed and wake times in 24-hour format (for bedtimes, for instance, "23:00" is 11PM, "0:00" is midnight, "1:00" is 1AM; for wake times, "9:00" is 9AM). I thought the difftime function would be good to extract the sleep duration, but I am running into an issue with some of the cases.

df$duration2<-difftime(df$Waketime2, df$Bedtime2, units="hours")

In running the syntax above, it correctly calculates duration for the cases where the person went to bed at Midnight of later. The problem arises when the bedtime is before midnight - (I think) because it is technically a time on a different day which screws things up. For those cases, this syntax will return a negative number.

(difftime (8:00, 0:00)) 

would return 8, but

(difftime (6:45,23:00)) 

would return -16.25.

I do not have data for the date that corresponds to the time, only the time values, but I know that the sequence is such that if one went to bed after midnight it's assumed that the wake time would be on that same day.

Thank you for any help!!

This function makes sense, but it is giving me 0 values for the time difference in hours (at first it showed 0 for all duration in seconds, so that's why I changed it to hours in my code below). I'm also pasting the structure of my bed and wake time variables in hope it helps.

sleepTime <- function(bed, wake){
  wake <- paste(Sys.Date(), wake)
  tmpbed <- paste(Sys.Date(), bed)
  adjust <- -(difftime(wake, tmpbed) < 0)
  tmpbed <- paste(Sys.Date() + adjust, bed)
  difftime(wake, tmpbed, units="hours")
}

df$Duration2<-sleepTime(df$Bedtime2, df$Waketime2)
df$sTST2
Time differences in hours
  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [67] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[133] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
str(S2trim$Bedtime2)

 'ITime' int [1:436] 23:00:00 NA 23:00:00 21:00:00 22:30:00 21:30:00 00:00:00 22:30:00 23:45:00 22:30:00 ...
str(S2trim$Waketime2)
 'ITime' int [1:436] 06:45:00 06:05:00 07:00:00 10:00:00 06:30:00 05:30:00 07:15:00 07:10:00 NA 05:30:00 ...

回答1:


This is a hack but it works. At least it does with the posted input data.

sleepTime <- function(bed, wake){
  wake <- paste(Sys.Date(), wake)
  tmpbed <- paste(Sys.Date(), bed)
  d <- apply(data.frame(tmpbed, wake), 1, function(x) difftime(x[2], x[1], units = "hours"))
  adjust <- -(d < 0)
  tmpbed <- paste(Sys.Date() + adjust, bed)
  apply(data.frame(tmpbed, wake), 1, function(x) difftime(x[2], x[1], units = "hours"))
}

bedtime2 <- c("0:00", "23:00")
waketime2 <- c("8:00", "6:45")
sleepTime(bedtime2, waketime2)
#[1]  8.00 7.75

Another example, with the data in the str outputs posted in the question.

sleepTime(bedtime, waketime)
# [1]  7.750000  6.083333  8.000000 13.000000  8.000000  8.000000
# [7]  7.250000  8.666667  0.250000  7.000000

Data.

bedtime <-
c("23:00:00", NA, "23:00:00", "21:00:00", "22:30:00", "21:30:00", 
"00:00:00", "22:30:00", "23:45:00", "22:30:00")

waketime <-
c("06:45:00", "06:05:00", "07:00:00", "10:00:00", "06:30:00", 
"05:30:00", "07:15:00", "07:10:00", NA, "05:30:0")


来源:https://stackoverflow.com/questions/60268964/calculating-time-duration-from-two-time-values-with-no-date-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!