Skip to content

Commit

Permalink
Function updated
Browse files Browse the repository at this point in the history
  • Loading branch information
masiraji committed Jan 8, 2024
1 parent 0008661 commit 2cbebbc
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions R/bedtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@
#'Calculates time spent in bed in hours. Output class is numeric.

#' @export
bedTime <- function(x,y)
{
bed <- x
wake <- y
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"))
}

bedTime <- function(x, y) {
if (is.na(x) | is.na(y)) {
warning("Either bedtime or waking time was NA. Hours in bed returned as NA")
return(NA)
} else if (grepl("[0-9][0-9]:[0-9][0-9]", x) | grepl("[0-9][0-9]:[0-9][0-9]", y)) {
bed <- x
wake <- y
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)
bed_hours <- apply(data.frame(tmpbed, wake), 1, function(x) difftime(x[2], x[1], units = "hours"))
return(bed_hours)
} else {
warning("Either bedtime or waking time is not a string variable, or is not formatted as 'hh:mm'")
return(NA)}
}

0 comments on commit 2cbebbc

Please sign in to comment.