0

I am trying to turn a table I have created below into a matrix that looks like the second table.

df <- data.frame("Group" = c(rep("G1", 5), rep("G2", 5), rep("G3", 5)),
           "Category" = c(rep(1:5, 3)),
           "Value" = c(9.5, 16.8, 38.5, 32.4,  2.8,  4.2, 36.1, 23.6, 34.7, 1.4, 8.9, 43.3, 20.0, 27.8, 0))

Group Category Value
   G1        1   9.5
   G1        2  16.8
   G1        3  38.5
   G1        4  32.4
   G1        5   2.8
   G2        1   4.2
   G2        2  36.1
   G2        3  23.6
   G2        4  34.7
   G2        5   1.4
   G3        1   8.9
   G3        2  43.3
   G3        3  20.0
   G3        4  27.8
   G3        5   0.0

I'm hoping there's an easy way to turn the category column into column headers so the table looks as such:

Group    1      2      3      4     5
    1  9.5   16.8   38.5   32.4   2.8
    2  4.2   36.1   23.6   34.7   1.4
    3  8.9   43.3   20.0   27.8   0.0
Andrew
  • 87
  • 7

0 Answers0