数据表的dcast没有返回预期的结果

时间:2016-08-26 18:03:43

标签: r data.table

使用以下data.table:

dt_cpu_[2000:2010,]
                    tim   source type_instance       val
 1: 2016-08-22 05:16:12.336  DBNode3        system     29471
 2: 2016-08-22 05:35:46.788  DBNode1         steal         0
 3: 2016-08-22 05:16:12.336  DBNode3         steal         0
 4: 2016-08-22 05:35:46.788  DBNode1          nice    617733
 5: 2016-08-22 05:16:12.336  DBNode3          wait       452
 6: 2016-08-22 05:16:12.336  DBNode3          idle 401793528
 7: 2016-08-22 05:16:12.336  DBNode3         steal         0
 8: 2016-08-22 05:16:02.336  DBNode3       softirq         0
 9: 2016-08-22 05:16:02.336  DBNode3         steal         0
 10: 2016-08-22 05:36:28.926 DNSFront          wait    211213
 11: 2016-08-22 05:36:28.926 DNSFront          nice    661508

使用以下类型:

  str(dt_cpu_)
Classes ‘data.table’ and 'data.frame':  10576673 obs. of  4 variables:
$ tim          : POSIXct, format: "2016-08-22 05:06:48.901" 
$ source       : chr  "DNSBack" "DBNode3" "DNSBack" "DBNode2" ...
$ type_instance: chr  "softirq" "user" "system" "user" ...
$ val          : num  0 99732 829061 171631 15111 ...

我使用以下代码来转换data.table:

dt_cpu_cast <- dcast.data.table(dt_cpu_[2000:2010,], tim + source ~ type_instance,  drop = TRUE, value.var="val") 

我希望得到如下输出:

                       tim   source system   steal  nice   wait    idle   softirq
 1: 2016-08-22 05:16:12.336  DBNode3  29471  0     NA     452   401793528  NA
 1: 2016-08-22 05:35:46.788  DBNode1  NA     0     617733 NA    NA         NA

但我得到以下内容:

                     tim   source    idle  nice  softirq steal system wait
1: 2016-08-22 05:16:02.336  DBNode3    0    0       1     1      0    0
2: 2016-08-22 05:16:12.336  DBNode3    1    0       0     2      1    1
3: 2016-08-22 05:35:46.788  DBNode1    0    1       0     1      0    0
4: 2016-08-22 05:36:28.926 DNSFront    0    1       0     0      0    1

我不明白我做错了什么。你能给我一些指导吗?

0 个答案:

没有答案