将缺少的日期插入数据框

时间:2013-02-08 16:24:45

标签: r

我有这个数据框:

      Date    Company           Region Units
1  1/1/2012        IBM          America    10
2  1/1/2012        IBM           Europe     4
3  1/1/2012        IBM          Pacific     2
4  1/1/2012         HP          America    10
5  1/1/2012         HP           Europe     2
6  1/1/2012    Gateway         Americas     2
7  1/2/2012        IBM         Americas    10
8  1/2/2012         HP           Europe     2
9 1/12/2012    Gateway         Americas    10

dput(x)
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 
3L, 2L), .Label = c("1/1/2012", "1/12/2012", "1/2/2012"), class = "factor"), 
    Company = structure(c(3L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 4L), .Label = c("   Gateway", 
    "   HP", "   IBM", "  Gateway"), class = "factor"), Region = structure(c(3L, 
    5L, 6L, 1L, 2L, 7L, 4L, 2L, 7L), .Label = c("         America", 
    "         Europe", "        America", "        Americas", 
    "        Europe", "        Pacific", "    Americas"), class = "factor"), 
    Units = c(10L, 4L, 2L, 10L, 2L, 2L, 10L, 2L, 10L)), .Names = c("Date", 
"Company", "Region", "Units"), class = "data.frame", row.names = c(NA, 
-9L))

我想创建一个热图,但是有很多缺少的日期,所以看起来不太好。我需要填写每个缺失区域和日期的单位。

我需要为每个公司和每个日期分别设置3个区域,如果缺少日期和区域,请插入并为单位设置0。

我可以在2012年1月1日至2012年1月12日期间为所有日期创建此向量:

d<-seq(as.Date(c("1/1/2012"), format="%m/%d/%Y"), as.Date(c("12/12/2012"), format="%m/%d/%Y"), by="mon")

对于每个公司,我必须检查所有三个区域的向量d存在的日期,如果没有插入单位0。

有一种简单的方法吗?任何指导将不胜感激。

1 个答案:

答案 0 :(得分:3)

如果您事先不了解公司区域或日期值,则可以使用expand.grid

> a <- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L,
+ 3L, 2L), .Label = c("1/1/2012", "1/12/2012", "1/2/2012"), class = "factor"),
+ Company = structure(c(3L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 4L), .Label = c("   Gateway",
+ "   HP", "   IBM", "  Gateway"), class = "factor"), Region = structure(c(3L,
+ 5L, 6L, 1L, 2L, 7L, 4L, 2L, 7L), .Label = c("         America",
+ "         Europe", "        America", "        Americas",
+ "        Europe", "        Pacific", "    Americas"), class = "factor"),
+ Units = c(10L, 4L, 2L, 10L, 2L, 2L, 10L, 2L, 10L)), .Names = c("Date",
+ "Company", "Region", "Units"), class = "data.frame", row.names = c(NA,
+ -9L))
> 
> b <- expand.grid(Date=unique(a$Date), Company=unique(a$Company), Region=unique(a$Region))
> 
> 
> 
> z <- merge(x=b,y=a, all.x=T)
> 
> z[is.na(z)] <- 0
> z
        Date    Company           Region Units
1   1/1/2012    Gateway          America     0
2   1/1/2012    Gateway           Europe     0
3   1/1/2012    Gateway          America     0
4   1/1/2012    Gateway         Americas     0
5   1/1/2012    Gateway           Europe     0
6   1/1/2012    Gateway          Pacific     0
7   1/1/2012    Gateway         Americas     2
8   1/1/2012         HP          America    10
9   1/1/2012         HP           Europe     2
10  1/1/2012         HP          America     0
11  1/1/2012         HP         Americas     0
12  1/1/2012         HP           Europe     0
13  1/1/2012         HP          Pacific     0
14  1/1/2012         HP         Americas     0
15  1/1/2012        IBM          America     0
16  1/1/2012        IBM           Europe     0
17  1/1/2012        IBM          America    10
18  1/1/2012        IBM         Americas     0
19  1/1/2012        IBM           Europe     4
20  1/1/2012        IBM          Pacific     2
21  1/1/2012        IBM         Americas     0
22  1/1/2012    Gateway          America     0
23  1/1/2012    Gateway           Europe     0
24  1/1/2012    Gateway          America     0
25  1/1/2012    Gateway         Americas     0
26  1/1/2012    Gateway           Europe     0
27  1/1/2012    Gateway          Pacific     0
28  1/1/2012    Gateway         Americas     0
29 1/12/2012    Gateway          America     0
30 1/12/2012    Gateway           Europe     0
31 1/12/2012    Gateway          America     0
32 1/12/2012    Gateway         Americas     0
33 1/12/2012    Gateway           Europe     0
34 1/12/2012    Gateway          Pacific     0
35 1/12/2012    Gateway         Americas     0
36 1/12/2012         HP          America     0
37 1/12/2012         HP           Europe     0
38 1/12/2012         HP          America     0
39 1/12/2012         HP         Americas     0
40 1/12/2012         HP           Europe     0
41 1/12/2012         HP          Pacific     0
42 1/12/2012         HP         Americas     0
43 1/12/2012        IBM          America     0
44 1/12/2012        IBM           Europe     0
45 1/12/2012        IBM          America     0
46 1/12/2012        IBM         Americas     0
47 1/12/2012        IBM           Europe     0
48 1/12/2012        IBM          Pacific     0
49 1/12/2012        IBM         Americas     0
50 1/12/2012    Gateway          America     0
51 1/12/2012    Gateway           Europe     0
52 1/12/2012    Gateway          America     0
53 1/12/2012    Gateway         Americas     0
54 1/12/2012    Gateway           Europe     0
55 1/12/2012    Gateway          Pacific     0
56 1/12/2012    Gateway         Americas    10
57  1/2/2012    Gateway          America     0
58  1/2/2012    Gateway           Europe     0
59  1/2/2012    Gateway          America     0
60  1/2/2012    Gateway         Americas     0
61  1/2/2012    Gateway           Europe     0
62  1/2/2012    Gateway          Pacific     0
63  1/2/2012    Gateway         Americas     0
64  1/2/2012         HP          America     0
65  1/2/2012         HP           Europe     2
66  1/2/2012         HP          America     0
67  1/2/2012         HP         Americas     0
68  1/2/2012         HP           Europe     0
69  1/2/2012         HP          Pacific     0
70  1/2/2012         HP         Americas     0
71  1/2/2012        IBM          America     0
72  1/2/2012        IBM           Europe     0
73  1/2/2012        IBM          America     0
74  1/2/2012        IBM         Americas    10
75  1/2/2012        IBM           Europe     0
76  1/2/2012        IBM          Pacific     0
77  1/2/2012        IBM         Americas     0
78  1/2/2012    Gateway          America     0
79  1/2/2012    Gateway           Europe     0
80  1/2/2012    Gateway          America     0
81  1/2/2012    Gateway         Americas     0
82  1/2/2012    Gateway           Europe     0
83  1/2/2012    Gateway          Pacific     0
84  1/2/2012    Gateway         Americas     0

注意:您的数据似乎有AmericaGateway等重复值。因此,在使用expand.grid

时,它们不止一次出现