Guest User

Untitled

a guest
Aug 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. #### RFM analysis #####
  2. library(dplyr)
  3. # filter to only United Kingdom
  4. data <- data[data$Country == "United Kingdom", ]
  5. data$PxQ <- data$Quantity * data$UnitPrice
  6. data$Recency <- difftime(as.Date(max(data$InvoiceDate)), as.Date(data$InvoiceDate), units = "days")
  7.  
  8. RFMresult <- data %>%
  9. select(InvoiceNo, CustomerID, Recency, PxQ) %>%
  10. group_by(CustomerID) %>%
  11. summarise(Recency = min(Recency), Frequency = n_distinct(InvoiceNo), Monetary = sum(PxQ)/n_distinct(InvoiceNo))
  12. RFMresult$Recency <- as.integer(RFMresult$Recency)
Add Comment
Please, Sign In to add comment