我试图弄清楚如何获取就寝时间的平均值和标准差,但我在午夜之后难以管理时间。这些概念在逻辑上是有道理的(我认为),但弄清楚如何在 R 中对其进行编码是很棘手的。
现在,那些将平均值拉低到白天(即,向中午),当他们在逻辑上应该将平均值拉向夜间(即,向午夜)。
这是一个示例,平均时间应该更接近午夜,但不是。
library(chron)
In.Bed.Date <- c("6/15/2019", "6/15/2019", "6/16/2019", "6/17/2019", "6/17/2019", "6/19/2019")
In.Bed.Time <- c("12:50 AM", "7:22 PM", "5:20 PM", "12:52 AM", "9:39 PM", "1:00 AM")
#Fix In Bed Date/Time Variables
In.Bed.Date <- as.dates(In.Bed.Date)
In.Bed.Time <- as.times(format(strptime(In.Bed.Time, '%I:%M %p'), format = '%H:%M:%S'))
#New Combined Date/Time Variable for Bed
In.Bed <- chron(dates. = In.Bed.Date, times. = In.Bed.Time, format = c(dates = 'm/d/y', times = 'h:m:s'))
#Mean/SD just using Time variable
Mean <- mean(In.Bed.Time)
SD <- times(sd(In.Bed.Time))
#Mean/SD trying to use Date/Time variable
Mean <- mean(In.Bed)
SD <- times(sd(In.Bed))
我遇到的问题是使用时间变量的平均值是在上午 10:10:30 出现的。我认为 SD 也错误地格式化了时间变量,因为它以 10:15:06 的形式出现,但我对此不太确定。
尝试使用日期/时间变量会导致计算包含日期,这也不是我想要的。相反,我只想找到每晚的平均就寝时间,以及就寝时间的 SD。