1

我有以下数据集(请参阅下面的加载数据集)

     ID       Date qty
1  ID25 2007-12-01  45
2  ID25 2008-01-01  26
3  ID25 2008-02-01  46
4  ID25 2008-03-01   0
5  ID25 2008-04-01  78
6  ID25 2008-05-01  65
7  ID25 2008-06-01  32
8  ID99 2008-02-01  99
9  ID99 2008-03-01   0
10 ID99 2008-04-01  99

我想创建一个数据透视表。我使用以下命令执行此操作,并且似乎工作正常:

pivottable <- xtabs(qty ~ ID + Date, table)

输出如下:

ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
ID25         45         26         46          0         78         65         32
ID99          0          0         99          0         99          0          0

但是,对于 ID99,只有 3 个周期的值,其余的被标记为“0”。我想在第一个表中没有值的字段中显示 NA 。我想得到一个如下所示的表格:

ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
ID25         45         26         46          0         78         65         32
ID99         NA         NA         99          0         99         NA         NA

关于如何做到这一点的任何建议?

加载数据集:

table <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("ID25", "ID99"), class = "factor"), Date = structure(c(7L, 
1L, 2L, 3L, 4L, 5L, 6L, 2L, 3L, 4L), .Label = c("01/01/2008", 
"01/02/2008", "01/03/2008", "01/04/2008", "01/05/2008", "01/06/2008", 
"01/12/2007"), class = "factor"), qty = c(45L, 26L, 46L, 0L, 
78L, 65L, 32L, 99L, 0L, 99L)), .Names = c("ID", "Date", "qty"
), class = "data.frame", row.names = c(NA, -10L))

table$Date <- as.POSIXct(table$Date, format='%d/%m/%Y')
4

1 回答 1

3

您可以使用xtabs两次来获得您正在寻找的输出:

  1. 创建表:

    pivottable <- xtabs(qty ~ ID + Date, table)
    
  2. 将不存在的组合的所有零替换为NA

    pivottable[!xtabs( ~ ID + Date, table)] <- NA
    

输出:

      Date
ID     2007-12-01 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01
  ID25         45         26         46          0         78         65         32
  ID99                               99          0         99                      

请注意,NA不显示 s。这是由于此类的print功能。但是您可以使用unclass(pivottable)来实现print.

于 2012-11-19T10:27:33.877 回答