1

I have the following data frame (called "graph_data"):

> graph_data
     dates types count
1  2010-01     A   171
2  2010-01     B   146
3  2010-01     C    55
4  2010-01     D    71
5  2010-01     E    66
6  2010-02     A    78
7  2010-02     B    56
8  2010-02     C    23
9  2010-02     D    21
10 2010-02     E    30
11 2010-03     A    43
12 2010-03     B    21
13 2010-03     C    18
14 2010-03     D    11

Using the reshape2 library, I was able to change this data into "long format":

dd <- graph_data
final = data.frame(reshape2::acast(dd, list(names(dd)[1], names(dd)[2])))

          A   B  C  D  E
2010-01 171 146 55 71 66
2010-02  78  56 23 21 30
2010-03  43  21 18 11 16
2010-04  32  20  7 14  9
2010-05  36  27  7 10 12

This worked fine - but now the "dates" column has disappeared:

> str(final)
'data.frame':   5 obs. of  5 variables:
 $ A: int  171 78 43 32 36
 $ B: int  146 56 21 20 27
 $ C: int  55 23 18 7 7
 $ D: int  71 21 11 14 10
 $ E: int  66 30 16 9 12

enter image description here

Is it possible to bring the "dates" column back? (i.e. 6 total columns)

How to fix this problem?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
stats_noob
  • 5,401
  • 4
  • 27
  • 83

0 Answers0