data_subset_1
Date Customer.Dimension Product.Dimension Channel.Dimension Sales.Price
1 Dec-13 Jan Phone FtF 2250
2 Dec-13 Koos Phone Web 1500
3 Dec-13 Jan Computer Web 3500
4 Dec-13 Koos Computer FtF 5250
5 Dec-13 Jan eBook FtF 375
6 Dec-13 Koos eBook Telephone 300
7 Dec-13 Koos Computer Web 3500
8 Dec-13 Jan Phone Web 1500
9 Dec-13 Piet Computer Web 3500
10 Dec-13 Koos Computer Telephone 4200
11 Dec-13 Jan Computer Web 3500
12 Dec-13 Koos Phone FtF 2250
13 Dec-13 Piet eBook Web 250
14 Dec-13 Koos Computer Web 3500
15 Dec-13 Koos Phone Web 1500
16 Jan-14 Koos eBook Web 250
17 Jan-14 Jan eBook Web 250
18 Jan-14 Koos Computer FtF 5250
19 Jan-14 Koos eBook Telephone 300
20 Jan-14 Jan eBook FtF 375
21 Jan-14 Koos Computer Web 3500
22 Jan-14 Koos Computer Web 3500
23 Jan-14 Koos Computer FtF 5250
24 Jan-14 Piet eBook Web 250
25 Jan-14 Piet eBook Web 250
26 Jan-14 Koos Computer Web 3500
27 Jan-14 Koos Phone Web 1500
28 Jan-14 Jan Computer Web 3500
29 Jan-14 Koos Phone Web 1500
30 Jan-14 Koos eBook FtF 375
31 Feb-14 Piet Computer Telephone 4200
32 Feb-14 Koos Phone Telephone 1800
33 Feb-14 Piet eBook Telephone 300
34 Feb-14 Koos eBook Telephone 300
35 Feb-14 Koos Computer Web 3500
36 Feb-14 Jan Computer FtF 5250
37 Feb-14 Jan Computer Web 3500
38 Feb-14 Jan Computer FtF 5250
39 Feb-14 Koos Phone Web 1500
40 Feb-14 Koos Phone Telephone 1800
41 Feb-14 Koos Computer Telephone 4200
42 Feb-14 Piet Computer Web 3500
43 Feb-14 Koos eBook Web 250
44 Feb-14 Jan Computer FtF 5250
45 Feb-14 Koos eBook FtF 375
46 Mar-14 Koos Computer FtF 5250
47 Mar-14 Piet Phone Web 1500
48 Mar-14 Koos Computer FtF 5250
49 Mar-14 Koos eBook FtF 375
50 Mar-14 Piet Phone FtF 2250
51 Mar-14 Piet Phone Telephone 1800
52 Mar-14 Koos eBook Telephone 300
53 Mar-14 Piet Phone Web 1500
54 Mar-14 Koos eBook Telephone 300
55 Mar-14 Jan Computer Web 3500
56 Mar-14 Jan Computer Web 3500
57 Mar-14 Piet Computer FtF 5250
58 Mar-14 Koos Computer FtF 5250
59 Mar-14 Koos Computer Telephone 4200
60 Mar-14 Jan Computer Telephone 4200
61 Apr-14 Piet Phone Web 1500
62 Apr-14 Koos Computer Web 3500
63 Apr-14 Jan Computer Web 3500
64 Apr-14 Koos Computer Telephone 4200
65 Apr-14 Piet Phone Web 1500
66 Apr-14 Koos Computer Web 3500
67 Apr-14 Koos Computer FtF 5250
68 Apr-14 Piet eBook Web 250
69 Apr-14 Koos Computer Web 3500
70 Apr-14 Koos Computer FtF 5250
71 Apr-14 Jan Phone Telephone 1800
72 Apr-14 Jan eBook Web 250
73 Apr-14 Koos Computer FtF 5250
74 Apr-14 Jan Computer FtF 5250
75 Apr-14 Koos Computer Web 3500
76 May-14 Koos eBook Web 250
77 May-14 Koos Phone Web 1500
78 May-14 Jan eBook Web 250
79 May-14 Piet eBook Telephone 300
80 May-14 Jan Phone Web 1500
81 May-14 Koos eBook Telephone 300
82 May-14 Piet eBook Web 250
83 May-14 Koos Computer Web 3500
84 May-14 Piet Computer FtF 5250
85 May-14 Piet eBook Telephone 300
86 May-14 Koos eBook Web 250
87 May-14 Jan eBook Web 250
88 May-14 Koos Phone FtF 2250
89 May-14 Piet eBook Telephone 300
90 May-14 Koos Computer Telephone 4200
z <- 1:nlevels( unique(data_subset_1$Product.Dimension) )
prod <- levels(data_subset_1$Product.Dimension)
m <- 1
conc_data_6_monhts <- list()
conc_data_3_monhts <- list()
prod_desc <- list()
while( m <= max(z) ) {
conc_data_6_monhts[[m]] <- mean( c( data_subset_1[ which( data_subset_1$Date == "May-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Apr-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Mar-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Feb-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Jan-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Dec-13" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"]
)
)
conc_data_3_monhts[[m]] <- mean( c( data_subset_1[ which( data_subset_1$Date == "May-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Apr-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"],
data_subset_1[ which( data_subset_1$Date == "Mar-14" & data_subset_1$Product.Dimension == prod[m] ), "Sales.Price"]
)
)
prod_desc[[m]] <- prod[m]
m <- m + 1
}
data_subset_1_means <- matrix(
data = c(conc_data_6_monhts, conc_data_3_monhts),
ncol = max(z),
byrow = FALSE,
dimnames = list(
c("6 Month Average", "3 Month Average"),
c( prod[z] )
)
)
我有一个看起来像
的矩阵 6 Month Average 3 Month Average
Computer 4148.905 4272.222
eBook 300.4237 289.5833
Phone 1778.571 1825.862
我希望得到一个分组的条形图。当我输入
时barplot( data_subset_1_means )
我得到一个堆积的条形图。当我将其更改为
时barplot( data_subset_1_means, beside = TRUE )
我收到以下错误:
Error in -0.01 * height : non-numeric argument to binary operator
关于我如何摆脱这个错误的任何建议?
答案 0 :(得分:1)
我不完全确定您收到错误的原因,但如果您在加入unlist
和conc_data_6_monhts
时使用conc_data_3_monhts
则问题就会消失。
所以:
data_subset_1_means <- matrix(
data = c(unlist(conc_data_6_monhts), unlist(conc_data_3_monhts)),
ncol = max(z),
byrow = FALSE,
dimnames = list(
c("6 Month Average", "3 Month Average"),
c( prod[z] )
)
)
顺便说一句,这样做要干净得多:
data_subset_1_means <- cbind(unlist(conc_data_6_monhts),
unlist(conc_data_3_monhts))
colnames(data_subset_1_means) <- c("6 Month Average", "3 Month Average")
rownames(data_subset_1_means) <- prod[z]
此外,我建议您将日期转换为POSIXct
个对象(您需要在前面添加一天,我假设是本月的第一天)
data_subset_1$Date <- as.POSIXct(paste0("01-",data_subset_1$Date),
format="%d-%b-%Y")
然后,您可以更轻松地过滤数据,例如使用aggregate
来计算均值。
例如,有些内容:
data_subset_1$BeforeMarch <- data_subset_1$Date<as.POSIXct("14-03-01")
aggregate(data_subset_1$Sales.Price, by=list(data_subset_1$BeforeMarch),
FUN=mean)