I stuck with creating a sparse matrix, in which I can count cross-selling frequency of products based on the cart and product ids.

Sample data frame:

```
x = data.frame(
      cart_id = c("1","1","1","2","2","3","4","5","5","6"),
      product_id = c("A","B","C","D","A","F","G","A","C","F")
)

```

The most desirable output: a sparse matrix with the number of times that two of products appeared in the same cart.

[![enter image description here](https://i.stack.imgur.com/QbkL3m.png)](https://i.stack.imgur.com/QbkL3m.png)

Any hints?

**EDIT:**

Both answers solve the problem.