There must be a simple and elegant way of doing this in R with data.table package, but I have trouble figuring it out. Vectorized operations are preferable.
library(data.table)
d1 <- as.Date("01-13-2013", '%m-%d-%Y')
d2 <- as.Date("12-31-2013", '%m-%d-%Y')
data <- data.frame(Date=sample(seq(d1,d2,1),10), Customer_ID=sample(1:5,20,replace=T), Product=sample(letters[1:5]), Store=sample(c("S1","S2")))
create
ID Date_Prod_A Times_Purchased_A Date_Prod_C Times_Purchased_C
1 47 01-01-2012 2 - -
2 26 - - 01-17-2012 1
Find the list of all values for product and add two columns per each product. Also, create a table that shows how many different products appear for each ID.
table <- data.table(data)