从R中没有日期的两个时间值计算持续时间 [英] Calculating time duration from two time values with no date in R

查看:54
本文介绍了从R中没有日期的两个时间值计算持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据日记中两次自我报告的时间来计算睡眠时间(以小时为单位).我有24小时制的起床和起床时间(例如,就寝时间而言,"23:00"是11PM,"0:00"是午夜,"1:00"是1AM;对于起床时间,"9:00是9AM.我认为difftime函数可以很好地提取睡眠时间,但是在某些情况下我遇到了问题.

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)) 

将返回8,但

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

将返回-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.

谢谢您的帮助!!

该函数很有意义,但是它为我提供了0个小时的时差值(起初它以秒为单位显示了所有持续时间的0,这就是为什么我在下面的代码中将其更改为hour).我还粘贴了床的结构和唤醒时间变量,希望对您有所帮助.

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 ...

推荐答案

这是一个hack,但可以使用.至少它与发布的输入数据有关.

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

另一个示例,问题中发布了 str 输出中的数据.

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

数据.

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")

这篇关于从R中没有日期的两个时间值计算持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆