R/calculateRFM.R

Defines functions calculateRFM

Documented in calculateRFM

#' calculate RFM
#'
# Description
#' @details
#' \code{data} contains the data
#'
#' Return value
#' @return Returns a data table containing...


calculateRFM <- function(transactions, weight_recency=1, weight_frequency=1, weight_monetary=1){



  #Bring the date into POSIXct format. ####/
  transactions[, TransDate:=dmy(TransDate, tz="UTC")]


  # Ensure that the weights add up to one
  weight_recency2 <- weight_recency/sum(weight_recency, weight_frequency, weight_monetary)
  weight_frequency2 <- weight_frequency/sum(weight_recency, weight_frequency, weight_monetary)
  weight_monetary2 <- weight_monetary/sum(weight_recency, weight_frequency, weight_monetary)

  # RFM measures
  max.Date <- max(transactions$TransDate)
  temp <- data[,list(
    recency = as.numeric(max.Date - max(TransDate)),
    frequency = .N,
    monetary = sum(PurchAmount)/.N),
    by="Customer"
    ]

  # RFM scores
  temp <- temp[,list(Customer,
    recency = as.numeric(cut2(-recency, g=3)),
    frequency = as.numeric(cut2(frequency, g=3)),
    monetary = as.numeric(cut2(monetary, g=3)))]

  # Overall RFM score
  temp[,finalscore:=weight_recency2*recency+weight_frequency2*frequency+weight_monetary2*monetary]

  # RFM group
  temp[,group:=round(finalscore)]

  # Return final table
  return(temp)
}
serkel/myPackage documentation built on May 29, 2019, 6:58 p.m.