Calculating mean and sd of bedtime (hh:mm) in R - problem are times before/after midnight

自作多情 提交于 2021-01-27 18:21:33

问题


I got the following dataset:

data <- read.table(text="
wake_time sleep_time
08:38:00  23:05:00  
09:30:00  00:50:00  
06:45:00  22:15:00  
07:27:00  23:34:00  
09:00:00  23:00:00  
09:05:00  00:10:00  
06:40:00  23:28:00  
10:00:00  23:30:00  
08:10:00  00:10:00  
08:07:00  00:38:00", header=T)

I used the chron-package to calculate the average wake_time:

> mean(times(data$wake_time))
[1] 08:20:12

But when I do the same for the variable sleep_time, this happens:

> mean(times(data$sleep_time))
[1] 14:04:00

I guess the result is distorted because the sleep_time contains times before and after midnight.

But how can I solve this problem?

Additionally: How can I calculate the sd of the times. I want to use it like "mean wake-up-time 08:20 ± 44 min" for example.

Thanks in advance, I appreciate any kind of help.

Cheers


回答1:


THe times values are stored as numbers 0-1 representing a fraction of a day. If the sleep time is earlier than the wake time, you can "add a day" before taking the mean. For example

library(chron)
wake <- times(data$wake_time)
sleep <- times(data$sleep_time)
times(mean(ifelse(sleep < wake, sleep+1, sleep)))
# [1] 23:40:00

And since the values are parts of a day, if you want the sd in minutes, you'd take the partial day values and convert to minutes

sd(ifelse(sleep < wake, sleep+1, sleep) * 24*60)
# [1] 47.60252


来源:https://stackoverflow.com/questions/65637024/calculating-mean-and-sd-of-bedtime-hhmm-in-r-problem-are-times-before-after

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