R/countBackContinous.R

Defines functions countBackContinuous

#' InflectionPoint
#'
#' @param data add description
#' @param countAfterVariable add description
#' @param nestingTimeUnit add description
#' @return df dataframe
#' @export
#' 
countBackContinuous <- function(data, countAfterVariable, nestingTimeUnit) {
  df <- data
  df$row <- row(df)
  df$row <- as.numeric(df$row)
  df$continuousTime <- countAfterVariable
  df$continuousTime <- as.numeric(df$continuousTime)
  df$continuousTimeBack <- df$continuousTime
  df$nestingUnit <- nestingTimeUnit
  continuousTimeBackCol <- match('continuousTimeBack', names(df))
  nestingUnitCol <- match('nestingUnit', names(df))
  index <- with(df, order(row, decreasing = TRUE))
  df <- df[index, ]
  for (i in 1:dim(df)[1]) {
    for (j in 1:dim(df)[2]) {
      if (j == continuousTimeBackCol) {
        if(i != 1) {
          if (df[i, nestingUnitCol] == df[(i - 1), nestingUnitCol]) {
            if (df[(i - 1), j] != '-999' & df[(i - 1), j] != -999) {
              if (df[(i - 1), j] <= 0) {
                if(df[(i - 1), j] == 0) {
                  df[i, j] <- (-1*(df[(i - 1), 'timeSince']))
                } else {
                  n1 <- df[(i - 1), continuousTimeBackCol]
                  n1 <- as.numeric(n1)
                  n2 <- df[(i - 1), 'timeSince']
                  n2 <- as.numeric(n2)
                  df[i, j] <- (n1 - n2)
                }
              }
            }
          }
        }
      }
    }
  }
  index <- with(df, order(row, decreasing = FALSE))
  df <- df[index, ]
  df[df == -999] <- NA
  df
}
enaY15/MultilevelFunctions documentation built on Aug. 22, 2020, 4:42 p.m.