1

I have a dataframe that has this structure

ID LINEITEM
1  shirt1
1  shirt2
2  shirt1
3  shirt3
3  shirt4
4  shirt5
5  shirt1
5  shirt2
5  shirt6

How can I shape the data in order to create edges and nodes between the LINEITEMS according to how many times they appear on the same ID?

My expected output would be:

       Shirt1 Shirt2 Shirt3 Shirt4 Shirt5 Shirt6
Shirt1   0      2       0       0      0       1   
Shirt2   2      0       0       0      0       0   
Shirt3   0      0       0       1      0       0   
Shirt4   0      0       1       0      0       0   
Shirt5   0      0       0       0      0       0   
Shirt6   1      1       0       0      0       0      

which counts every time two shirts appear on a same ID together)

1 Answers1

3

Check with table

table(df$ID, df$LINEITEM)

As r2evans mentioned in comments

xtabs(~ ID + LINEITEM, data = df)
BENY
  • 317,841
  • 20
  • 164
  • 234