-1

I have a data frame in 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 to a table:

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

Is there an easy way of doing this other than manually?

Nakx
  • 1,460
  • 1
  • 23
  • 32
user98235
  • 830
  • 1
  • 13
  • 31

2 Answers2

5

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
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You may use function acast from library(reshape2):

data<-data.frame(state1,state2,score)
library(reshape2)
datan<-acast(data,state1 ~ state2)