user98235 user98235 - 1 month ago 10
R Question

Converting a data frame to table

I have the data frame having the following format:

state1 state2 score
A A 3
A B 13
A C 5
B A 1
B B 0
B C 0
C A 5
C B 6
C C 3


I would like to convert it into a table having the format

A B C
A 3 13 5
B 1 0 0
C 5 6 3


Is there an easy way of doing it instead of creating it manually?

Answer

We can do this with xtabs

xtabs(score~state1+state2, df1)
#        state2
#state1  A  B  C
#     A  3 13  5
#     B  1  0  0
#     C  5  6  3
Comments