R/time_to_minutes.R

Defines functions time_mins time_to_minutes

Documented in time_to_minutes

#' time_to_minutes
#'
#' Read an Excel time column and convert it to a numeric value for minutes.
#' @param x Your data frame
#' @param colname The name of the column to convert
#' @export

time_to_minutes <- function(x,colname) {

  require(tidyverse)

  #note: this function allows you to pass the column name as an argument and then transform the data according to your needs
  z <- x %>%
    mutate(hours = as.numeric(str_sub(x[[colname]],start=1,end=2)),
           minutes = as.numeric(str_sub(x[[colname]],start=4,end=5)),
           seconds = as.numeric(str_sub(x[[colname]],start=-2)),
           total_minutes = (hours * 60) + minutes + (seconds / 60)) %>%
    select(-hours,-minutes,-seconds)

  return(z)
}

time_mins <- function(x) {

hours = as.numeric(str_sub(x,start=1,end=2))
minutes = as.numeric(str_sub(x,start=4,end=5))
seconds = as.numeric(str_sub(x,start=-2))

total_minutes = round((hours * 60) + minutes + (seconds / 60),digits=2)

  return(total_minutes)

}
neugelb/neugelbtools documentation built on July 7, 2020, 1:17 a.m.