I have a dataframe like below
id <- c(1,1,1,2,2,2,1,3,4,4)
product <- c("a","b","c","a","d","f","e","f","e","f") 
df <- data.frame(id,product)
   id product
1   1       a
2   1       b
3   1       c
4   2       a
5   2       d
6   2       f
7   1       e
8   3       f
9   4       e
10  4       f
I want to transform it to a dataframe as below.
id a b c d e f
1  1 1 1 0 1 0
2  1 0 0 1 0 1
3  0 0 0 0 0 1
4  0 0 0 0 1 1
Essentially, I need only one record for each id and the record should contain 0 or 1 depending on whether a product is purchased or not. I used model.matrix, but it does not group by id, I get 10 rows as in the original dataset.