Search code examples
rdplyraggregate

Find closest waking up time to a trigger time


I have a dataset where I have two types of data, data from a sensor and data from an app for ecological momentary assessment. Both the sensor data and the EMA data were collected for 4 days at 2 timepoints. The sensor was supposed to be worn for most of the day, inlcuding when sleeping. So I have information on when someone was sleeping, awake or not wearing the sensor. On the other hand, participants were also told to push the button on the EMA app as soon as they woke up. So I have waking up informating from two sources, the sensor data and the app Trigger. Given that the sensor data is more objective, I would like to use that as my measure of waking time. What I am trying to do now is to take the waking up time from sensor data that is closest to the first app trigger of the day. However, I am unable to extract the times.

Using one participant as an example I am going to detail my steps. I am working in R.

  1. First, I derive the objective waking up times for all 8 days based on the transition from sleep (1) to awake (0):
library(dplyr)
data$Time <- as.POSIXct(data$Time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
wake_up_rows <- which(data$SleepWakeWear == 0 
                      & lag(data$SleepWakeWear) == 1)
wake_up_times <- data$Time[wake_up_rows]

This gives me the following POSIXct vector:

wake_up_times <- 
structure(c(1513387680, 1513398780, 1513407000, 1513470900, 1513473180, 
1513487820, 1513492560, 1513551420, 1513555740, 1513563180, 1513574940, 
1513615500, 1513639080, 1513649280, 1513652760, 1513664280, 1513667520, 
1513696980, 1513716480, 1527127381, 1527137761, 1527183721, 1527198121, 
1527217501, 1527225841, 1527291841, 1527296161, 1527299041, 1527302701, 
1527313021, 1527317041, 1527386521, 1527388801, 1527396061, 1527405121), 
class = c("POSIXct", "POSIXt"), tzone = "UTC")
  1. Then I derive the first trigger times from EMA app data:
data$Date_only <- as.Date(data$Time)
data$Trigger_time <- as.POSIXct(data$Trigger_time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
first_trigger_times <- aggregate(Trigger_time ~ Date_only, data = data, FUN = min, na.rm = TRUE)

This gives me the following table:

> print(first_trigger_times)
   Date_only     Trigger_time
1 2017-12-16     2017-12-16 06:52:11
2 2017-12-17     2017-12-17 06:38:45
3 2017-12-18     2017-12-18 05:37:12
4 2017-12-19     2017-12-19 07:13:43
5 2018-05-24     2018-05-24 05:33:56
6 2018-05-25     2018-05-25 06:27:33
7 2018-05-26     2018-05-26 06:44:41
8 2018-05-27     2018-05-27 07:13:45
  1. Now I am trying to find the objective waking up time (from 1) that is closest to the trigger time (in 2).
first_trigger_times$ClosestWakeUpTime <- sapply(first_trigger_times$Trigger_time, function(trigger_time) {
  closest_wake_up_time <- max(wake_up_times[wake_up_times <= trigger_time])
  return(closest_wake_up_time)
})

However, I don't end up getting the date-time combination (from 1) in column 3:

> print(first_trigger_times)
   Date_only     Trigger_time            ClosestWakeUpTime
1 2017-12-16     2017-12-16 06:52:11        1513407000
2 2017-12-17     2017-12-17 06:38:45        1513492560
3 2017-12-18     2017-12-18 05:37:12        1513574940
4 2017-12-19     2017-12-19 07:13:43        1513667520
5 2018-05-24     2018-05-24 05:33:56        1527137761
6 2018-05-25     2018-05-25 06:27:33        1527225841
7 2018-05-26     2018-05-26 06:44:41        1527317041
8 2018-05-27     2018-05-27 07:13:45        1527405121

I would expect a date-time combination to show up in "closestWakeUpTime". For example for Trigger_time 2017-12-16 06:52:11, I would expect 2017-12-16 06:50:00 UTC to appear in ClosestWakeUpTime column

What is incorrect in the above code? I would really appreciate any and all help.


Solution

  • ClosestWakeUpTime needed to be in POSIXct format. To turn it into POSIXctformat, I used:

    first_trigger_times$ClosestWakeUpTime <- as.POSIXct(first_trigger_times$ClosestWakeUpTime, origin = "1970-01-01", tz = "UTC")