根据唯一列值生成新表

时间:2016-09-09 11:18:31

标签: r data.table

我有一个data.table,其格式如下:

          date           action cnt
 1: 2016-06-14      PICK_APPLE   2
 2: 2016-06-15      PICK_APPLE  12
 3: 2016-06-16      PICK_APPLE   7
 4: 2016-06-17      PICK_APPLE   5
 5: 2016-08-10      PICK_APPLE   1
 6: 2016-08-11      PICK_APPLE   1
 7: 2016-08-14      PICK_APPLE   1
 8: 2016-08-17      PICK_APPLE   7
 9: 2016-08-19      PICK_APPLE   1
10: 2016-08-21      PICK_APPLE   2
11: 2016-08-22      PICK_APPLE   3
12: 2016-08-23      PICK_APPLE  12
13: 2016-08-24      PICK_APPLE   6
14: 2016-08-30      PICK_APPLE   4
15: 2016-08-31      PICK_APPLE   4
16: 2016-09-05      PICK_APPLE   8
17: 2016-09-08      PICK_APPLE   1
18: 2016-08-23     PICK_ORANGE   1
19: 2016-08-31     PICK_ORANGE   2
20: 2016-09-05     PICK_ORANGE   2
21: 2016-08-05       ATE_APPLE   1
22: 2016-08-14       ATE_APPLE   1
23: 2016-08-17       ATE_APPLE   1
24: 2016-08-18       ATE_APPLE   1
25: 2016-08-30       ATE_APPLE   3
      date           action     cnt

我想创建一个表,其中列名称为action列中的唯一值,行名称为date列中的唯一值。例如,结果应该是:

          date           PICK_APPLE PICK_ORANGE ATE_APPLE
 1: 2016-06-14               2
 2: 2016-06-15              12
 3: 2016-06-16               7
 4: 2016-06-17               5
21: 2016-08-05                                      1
 5: 2016-08-10               1
 6: 2016-08-11               1
 7: 2016-08-14               1                      1
 8: 2016-08-17               7                      1
24: 2016-08-18                                      1
 9: 2016-08-19               1
10: 2016-08-21               2
11: 2016-08-22               3
12: 2016-08-23              12         1
13: 2016-08-24               6
14: 2016-08-30               4
15: 2016-08-31               4         2
16: 2016-09-05               8
17: 2016-09-08               1
20: 2016-09-05                         2
          date           PICK_APPLE PICK_ORANGE ATE_APPLE

是否有标准方法可以执行此操作,或者我是否需要循环执行每个(date,action)组合?

0 个答案:

没有答案