R/countAfterEvent.R

Defines functions countAfterEvent

#' InflectionPoint
#'
#' @param data add description
#' @param eventVariable add description
#' @param nestingTimeUnit add description
#' @return df dataframe
#' @export
#' 
countAfterEvent <- function(data, eventVariable, nestingTimeUnit) {
  df <- data
  df$event <- eventVariable # New generic variable named 'event'
  df$nestingUnit <- nestingTimeUnit # Nesting unit within which count is cycling through (e.g. "day" containing multiple measurements)
  df$event <- as.numeric(df$event)
  eventCol <- match('event', names(df)) # Stored value = column number of 'y' in fx argument
  df$count <- count <- '-999' # Generic 'Count' variable set to -999 starting values
  df$count <- as.numeric(df$count)
  countCol <- match('count', names(df))
  nestingUnitCol <- match('nestingUnit', names(df))
  df$nestingUnit <- as.numeric(df$nestingUnit)
  for (i in 1:dim(df)[1]) { # Cycle through all rows in scope of df
    for (j in 1:dim(df)[2]) { # Cycle through all columns in scope of df
      if (j == countCol) {
        if (i == 1) { # If on first row, run this code to avoid (i - 1) problem
          if (df[i, eventCol] == 1) {
            df[i, 'count'] <- 0 # Count = 0 if measurement occasion conflict (+)
            #df[i, 'count'] <- as.numeric(df[i, 'count'])
          } else {
            if (df[i, eventCol] == 0) {
              df[i, j] <- df[i, j]
              #df[i, 'count'] <- as.numeric(df[i, 'count'])
            }
          }
        } else { # Now not on first row so (i - 1) term not a problem
          if (df[i, nestingUnitCol] != df[(i - 1), nestingUnitCol]) {
            if (df[i, eventCol] == 1) {
              df[i, countCol] <- 0
              #df[i, 'count'] <- as.numeric(df[i, 'count'])
            } else {
              df[i, countCol] <- df[i, j]
              #df[i, 'count'] <- as.numeric(df[i, 'count'])
            }
          } else {
            if (df[(i - 1), countCol] == -999) {
              if (df[i, eventCol] == 0) {
                df[i, j] <- df[i, j]
                #df[i, 'count'] <- as.numeric(df[i, 'count'])
              } else {
                if (df[i, eventCol] == 1) {
                  df[i, j] <- 0
                  #df[i, 'count'] <- as.numeric(df[i, 'count'])
                }
              }
            } else {
              n <- df[(i - 1), j]
              n <- as.numeric(n)
              df[i, j] <- (n + 1)
              #df$count <- df[i, 'count'] <- as.numeric(df[i, 'count'])
            }
          }
        }
      }
    }
  }
  df
}
enaY15/MultilevelFunctions documentation built on Aug. 22, 2020, 4:42 p.m.