R/makeTimeWindows.R

Defines functions makeTimeWindows

Documented in makeTimeWindows

#' Helper function that creates temporal windows in datasets
#' 
#' This creates a set of temporal windows of specified size so that metrics 
#' can be computed within those windows. 
#'
#' @param inputData data.frame that has data over time, usually within a single meeting 
#' @param timeVar name of a numeric column that contains the time variable you want to use
#' @param windowSize numeric value giving the length of time window 
#'
#' @return list with two data.frames:
#' \itemize{
#'     \item windowedData - inputData with the temporal window identifying information included
#'     \item allWindows - contains the full set of temporal windows and identifying information. This is valuable because inputData may not have records within all of the possible temporal windows
#'  }
#' @export
#'
#' @examples
#' win.out = makeTimeWindows(sample_transcript_processed, 
#' timeVar="utteranceStartSeconds", windowSize=10)
makeTimeWindows = function(inputData, timeVar, windowSize) {
  
  tempTimeVarHelp <- tempTimeVar <- NULL
  
  inputData$tempTimeVar = floor(inputData[,timeVar])
  
  # Create a set of windows
  windowStart = windowSize*(1:ceiling(max(inputData[,timeVar])/windowSize)-1)
  windowEnd = windowSize*(1:ceiling((max(inputData[,timeVar])/windowSize)))-1
  windowId = 1:length(windowEnd)
  windows = data.table::data.table(cbind(windowId, windowStart, windowEnd))
  
  # Join
  inputData.dt = data.table::data.table(inputData)
  inputData.dt[, tempTimeVarHelp := tempTimeVar]
  data.table::setkey(windows, windowStart, windowEnd)
  inputMrg = data.frame(foverlaps(inputData.dt, windows, by.x=c('tempTimeVar', 'tempTimeVarHelp'), 
                                  by.y=c('windowStart', 'windowEnd')))
  inputMrg[,timeVar] = inputMrg$tempTimeVar
  inputMrg[,c("tempTimeVar", "tempTimeVarHelp")] = NULL
  windows = data.frame(windows)
  return(list("windowedData" = inputMrg, "allWindows" = windows))
}

Try the zoomGroupStats package in your browser

Any scripts or data that you put into this service are public.

zoomGroupStats documentation built on May 13, 2021, 5:06 p.m.