R/seperate_date_and_time.R

Defines functions seperate_date_and_time

Documented in seperate_date_and_time

#' Seperate data and time functionality
#' 
#' This function can in principle seperate any amalgamated character vector column member of the data frame where elements are sepearted by " " space.
#' 
#' @param df Input data frame.
#' @param column_specifying_data Either a boolean with single TRUE case or a single numeric indicating what column is has date and time. 
#' @return An updated data frame where data-time column are seperate.
#' @export

seperate_date_and_time = function(df, column_specifying_data){
  if(class(column_specifying_data)=="numeric"){
    if(!length(column_specifying_data)==1){stop("If column_specifying_data is numeric it must me of lenght 1.")}
  }
  date_time = as.character(df[,column_specifying_data])
  date_time = unlist(strsplit(x = date_time, split = " ",fixed = T))
  date = date_time[seq(1,length(date_time),by = 2)]
  time = date_time[seq(2,length(date_time),by = 2)]
  df[,column_specifying_data] = NULL
  df = cbind(df,date,time)
  return(df)
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.