Closed muschellij2 closed 4 years ago
Option 2 would be much more straightforward, ideally as a follow-on to read_gt3x
, or possibly as a new argument (flag_sleep = FALSE
by default). Something like this:
flag_sleep <- function(RAW, EVENT) {
# Check that RAW and EVENT inherit from RAW and EVENT classes, respectively
# Initialize RAW$sleep <- FALSE
# If EVENT$idle_sleep_events has nrow == 0, return RAW
# Make lubridate intervals out of EVENT$idle_sleep_events[ ,c("sleep_ON", "sleep_OFF")]
# is_sleep <- sapply(RAW$Timestamp, function(x) any(lubridate::%within%(x, EVENT$idle_sleep_events$intervals))
# RAW$sleep <- ifelse(is_sleep, TRUE, FALSE)
# return RAW
}
I agree - like it - may call it something other than sleep
.
What about
#' @importFrom lubridate %within%
flag_sleep <- function(RAW, EVENT) {
if (is.null(RAW)) {
return(NULL)
}
if (is.null(EVENT)) {
stop("need to read in EVENT data as well as ACTIVITY")
}
if (NROW(EVENT$idle_sleep_events) == 0) {
return(RAW)
}
sleep_events = EVENT$idle_sleep_events[ ,c("sleep_ON", "sleep_OFF")]
intervals = mapply(interval,
sleep_events$sleep_ON,
sleep_events$sleep_OFF,
SIMPLIFY = FALSE)
RAW$idle = RAW$Timestamp %within% intervals
return(RAW)
}
Concept looks good to me. A few considerations:
I forget if the start and/or stop times are inclusive in idle_sleep_events
. My guess is they're both inclusive, which is ideal. Just need to note that %within%
treats the interval as inclusive on both sides, so it could be necessary to add/subtract 1 s from the start/end of each interval. (This would have to be tested by comparison against a RAW.csv
file. I believe idle sleep modes are handled by latching to the last "active sample", or whatever it should be called. You can then determine when the interval ends based on the next place a new value occurs.)
I don't recall off the top of my head how vector input behaves with %within%
. So I could be wrong, but I don't think the one-liner you have will work. My guess is each Timestamp
will be compared against a single element in intervals
, with the latter being recycled to match the length of the Timestamp
vector. Instead, I think it will need to be:
RAW$idle <- sapply(RAW$Timestamp, function(x) any(x %within% intervals))
Can you test this out and open a PR?
Would it be possible to either
NA
or0
? OR