1

I have a problem and I would ask if is a function or easy way to do below operation.

I have a data.frame like this

customer     item
-------------------
smith         a
smith         b
smith         c
johnson       a
bush          NA
regan         d

How to create matrix like this

customer    a   b  c  d 
--------------------------------------
smith       1   1  1  0
johnson     1   0  0  0
bush        0   0  0  0
regan       0   0  0  1

Is loop obligartory? Is easier way to create this? Thank you in advance!

Tensibai
  • 15,557
  • 1
  • 37
  • 57
ann
  • 19
  • 3

1 Answers1

0

You should use the table function. The call would look something like this. IT goes x,y but depending on what the full data.frame list looks you may want to add some more parameters to handle NA values and such

table(df$customer, df$item)
Adam
  • 648
  • 6
  • 18