Data preparation

In order to build a predictive model, we need to prepare our data first, so that we can feed in the relevant data into the model. Take a look at the following code:

# group data into every 3 monthslibrary(lubridate)ordersDF$Quarter = as.character(round_date(ordersDF$InvoiceDate, '3 months'))dataDF <- ordersDF %>%  group_by(CustomerID, Quarter) %>%  summarize(SalesSum=sum(Sales), SalesAvg=mean(Sales), SalesCount=n())

As you can see from this code, we are using the lubridate package that is going to help us to handle data with dates more easily. Using the round_date function in the lubridate package, we first round InvoiceDate to the nearest quarter. Then, we group the data by CustomerID and the newly-created column, Quarter ...

Get Hands-On Data Science for Marketing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.