#' Separate Dataframe by Time Window
#'
#' Function to separate the dataframe into a list by the time window specified.
#'
#' @param df Dataframe: first column time stamps in POSIXct format, second column sequence in factor format.
#' @param timetype Time window for separating the dataset, i.e. day = '%Y-%m-%d', week = '%Y-%W' and month '%Y-%m'.
#'
#' @return List of dataframes for each sequence separated by time window.
#' @export
#'
#'@details Simple function to separate a dataframe into a list of dataframes by the time window desired. The dataframe
#'must be in a specific format to be separated; first column - time stamps that provide enough information to adapt to the
#'chosen time window, second column - categorical sequence in factor format.
#'
#' @examples
#' df<-data.frame(Time=c("2020-01-01 00:10:09", "2020-01-01 01:12:34" , "2020-01-01 06:38:09",
#' "2020-01-01 07:21:51"),Cat=c('A','B','A','C'))
#' #For daily data:
#' SeqList(df,'%Y-%m-%d')
SeqList <- function(df, timetype) {
x <- as.POSIXlt(df[, 1])
tryCatch(strftime(x, format = timetype), error= function(e) print('First Column of dataframe doesnt conform with chosen timetype'))
df$DateNum<-strftime(x, format = timetype)
UniTim<-unique(df$DateNum)
NumRows <- length(UniTim)
sequencevector <- vector("list", NumRows)
j <- 1
for (i in 1:NumRows) {
sequencevector[[j]] <- as.data.frame(dplyr::filter(df, DateNum == UniTim[i]))
sequencevector[[j]] <- as.data.frame(dplyr::select(sequencevector[[j]], -DateNum))
if (plyr::empty(sequencevector[[j]])) {
sequencevector[[j]] <- cbind(0, 0)
colnames(sequencevector[[j]]) <- c("Date", "Sequence")
}
j <- j + 1
}
return(sequencevector)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.