Challenge #365 [Intermediate] Sales Commissions
# https://www.reddit.com/r/dailyprogrammer/comments/8xzwl6/20180711_challenge_365_intermediate_sales/?sort=new
## DATA
revenue <- matrix(data = c(190, 140, 1926, 14, 143, 325 ,19, 293,
1491, 162, 682, 14, 852, 56, 659, 829,
140, 609, 120, 87),
nrow = 4, byrow = T,
dimnames = list(c("Tea", "Coffee", "Water", "Milk"),
c("Johnver", "Vanston" ,"Danbree",
"Vansey", "Mundyke")))
expenses <- matrix(data = c(120, 65, 890, 54, 430, 300, 10, 23, 802,
235,50, 299, 1290, 12, 145, 67, 254, 89,
129, 76),
nrow = 4, byrow = T,
dimnames = list(c("Tea", "Coffee", "Water", "Milk"),
c("Johnver", "Vanston" ,"Danbree",
"Vansey", "Mundyke")))
## FUNCTION
coms <- function(rvn, exs, prc = 0.062){
profit <- revenue - expenses; profit[profit < 0] <- 0
comm.vec <- floor(colSums(profit * prc))
commissions <<- matrix(data = comm.vec, nrow = 1,
ncol = length(comm.vec),
dimnames = list("Commission", colnames(rvn)))
commissions
}
## CHALLENGE CASE
coms(rvn = revenue, exs = expenses)