这是我的原始输出。
BizDate Description TotalAmount TotalQty
2013-09-01 NIN JIOM COUGH SYRUP 75ML 11.8 2
2013-09-01 BREACOL COUGH SYRUP ADULTS 120ML 15.8 2
2013-09-02 AFRICAN SEA-COCONUT COUGH SYRUP 177ML 8.5 1
2013-09-03 AFRICAN SEA COCONUT COUGH SYRUP 177ML 8.2 1
2013-09-01 THREE LEGS COOLING WATER 200ML 21.0 14
2013-09-01 SEAHORSE BRAND COOLING WATER 200ML 4.4 4
2013-09-05 AFRICAN SEA-COCONUT COUGH SYRUP 177ML 8.5 1
2013-09-06 AFRICAN SEA COCONUT COUGH SYRUP 177ML 8.2 1
我怎么把它变成这样的东西
Description 2013-09-01 2013-09-02 .......
NIN JIOM COUGH SYRUP 75ML some number some number .......
BREACOL COUGH SYRUP ADULTS 120ML some number some number .......
AFRICAN SEA-COCONUT COUGH SYRUP 177ML some number some number .......
AFRICAN SEA COCONUT COUGH SYRUP 177ML some number some number .......
THREE LEGS COOLING WATER 200ML some number some number .......
SEAHORSE BRAND COOLING WATER 200ML some number some number .......
AFRICAN SEA-COCONUT COUGH SYRUP 177ML some number some number .......
AFRICAN SEA COCONUT COUGH SYRUP 177ML some number some number .......
这是一些假数据。
set.seed(1)
dat <- data.frame(product = rep(letters[1:4], each = 4)
, date = sample(seq(as.Date("2013-01-01"), as.Date("2013-01-10"), by = 1), 16, TRUE)
, amount = sample(1:100, 16, TRUE)
, qty = sample(1:4, 16, TRUE))
dat
product date amount qty
1 a 2013-01-03 72 2
2 a 2013-01-04 100 1
3 a 2013-01-06 39 4
4 a 2013-01-10 78 3
5 b 2013-01-03 94 4
6 b 2013-01-09 22 1
7 b 2013-01-10 66 3
8 b 2013-01-07 13 2
9 c 2013-01-07 27 4
10 c 2013-01-01 39 3
11 c 2013-01-03 2 4
12 c 2013-01-02 39 3
13 d 2013-01-07 87 3
14 d 2013-01-04 35 4
15 d 2013-01-08 49 1
16 d 2013-01-05 60 2
我这样做的愚蠢方式。有没有更聪明的方法来做到这一点?我正在考虑使用 plyr。只需将我引导到正确的包裹或方式。
dat1<-subset(dat,date=="2013-01-01")
colnames(dat1)[c(3,4)]<-c("2013-01-01.amount","2013-01-01.qty")
dat1<-dat1[-2]
dat2<-subset(dat,date=="2013-01-02")
colnames(dat2)[c(3,4)]<-c("2013-01-02.amount","2013-01-02.qty")
dat2<-dat2[-2]
dat3<-subset(dat,date=="2013-01-03")
colnames(dat3)[c(3,4)]<-c("2013-01-03.amount","2013-01-03.qty")
dat3<-dat3[-2]
new.dat<-merge(dat1,dat2,by="product",all.x=T,all.y=T)
new.dat<-merge(new.dat,dat3,by="product",all.x=T,all.y=T)
new.dat[is.na(new.dat)]<-0
new.dat
product 2013-01-01.amount 2013-01-01.qty 2013-01-02.amount 2013-01-02.qty
1 a 0 0 0 0
2 b 0 0 0 0
3 c 39 3 39 3
2013-01-03.amount 2013-01-03.qty
1 72 2
2 94 4
3 2 4