3

我有这个数据框:

      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 中的日期是否存在于所有三个区域中,如果不使用 Units 0 插入它。

是否有捷径可寻?任何指导将不胜感激。

4

1 回答 1

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

于 2013-02-08T16:53:36.237 回答