R/kpiCalculations.R

Defines functions calcRevenueShop calcCustomersShop calcNumProdsShop calcRevenueI calcQuantileI calcNumProdsI

Documented in calcCustomersShop calcNumProdsI calcNumProdsShop calcQuantileI calcRevenueI calcRevenueShop

#' Data Preparation Functions
#' @param ecomData data object
#' 
#' @export
#' @rdname kpiFunctions
calcRevenueShop <- function(ecomData) {
  format(round(ecomData %>% select("Sales") %>%  sum()), big.mark = " ")
}
#' @export
#' @rdname kpiFunctions
calcCustomersShop <- function(ecomData) {
  format(round(ecomData %>% summarise(
    UniqueElements = n_distinct(CustomerID))), big.mark = " ")
}

#' @export
#' @rdname kpiFunctions
calcNumProdsShop <- function(ecomData) {
  format(round(ecomData %>% summarise(
    UniqueElements = n_distinct(as.character(StockCode)))),
    big.mark = " ")
}

#' @export
#' @rdname kpiFunctions
calcRevenueI <- function(ecomData, customerID) {
  ecomData <- ecomData %>% filter(CustomerID == customerID)
  revenue <- format(round(ecomData %>% select("Sales") %>%  sum()),
                    big.mark = " ")
}
#' @export
#' @rdname kpiFunctions
calcQuantileI <- function(ecomData, customerID) {
  revenues <- ecomData %>% group_by(CustomerID) %>%
    summarise(revenue = sum(Sales))
  listRevenues <- revenues$revenue
  percentile <- ecdf(listRevenues)
  revenueI <- ecomData %>% filter(CustomerID == customerID) %>%
    group_by(CustomerID) %>% summarise(revenue = sum(Sales))
  revenueI <- revenueI$revenue
  100 - round(percentile(revenueI) * 100, 1)
}

#' @export
#' @rdname kpiFunctions
calcNumProdsI <- function(ecomData, customerID) {
  numProducts <- format(round(
    ecomData %>% filter(CustomerID == customerID) %>%
      summarise(UniqueElements = n_distinct(as.character(StockCode)))),
    big.mark = " ")
}
INWTlab/ecom-analytics documentation built on Sept. 9, 2019, 5:05 p.m.