R/ps_to_T_strict_stayer.R

Defines functions ps_to_T_strict_stayer

Documented in ps_to_T_strict_stayer

#' Calculates T from a purchase string under the "strict stayer" assumption.  
#' 
#' @param ps Purchase string.
#' @return The numeric value for T, which is the position of the last 1 in the purchase string
#' @export ps_to_T_strict_stayer

#Strict stayer assumes that any inactivity in the customer relationship is a pause.  Does not end the customer relationship.

ps_to_T_strict_stayer <- function(ps) {
  #print(ps)
  
  if(typeof(ps) != "character") {
    return(1)
  }
  
  a <- strsplit(ps, split = '')
  a <- unlist(a)
  a <- as.numeric(a)
  
  if(sum(a) > 0 & is.na(sum(a)) == FALSE) {
    cancellation_time <- max(which(a == 1))
  } else {
    cancellation_time <- 0
  }
  
  
  return(cancellation_time)
}

Try the CADF package in your browser

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

CADF documentation built on Oct. 31, 2024, 5:08 p.m.