我想将数据框从长到大的2个变量(其组合创建新的唯一标识符)重新整形,同时按产品,产品2和日期汇总值,以便以下内容:
Date product product2 value
03/03/2011 a z 7
03/03/2011 a z 2
03/05/2015 b z 89
03/01/2017 a z 2
03/03/2017 c z 6
将产生以下结果:
03/03/2011 03/03/2011 03/05/2015 03/01/2017 03/03/2017
a z 9 2
b z 89
c z 6
我应该使用dplyr,reshape,reshape2吗?
df <- structure(list(Date = c("03/03/2011", "03/03/2011", "03/05/2015", "03/01/2017", "03/03/2017"),
product= c("a", "a", "b", "a", "c"),
product2= c("z", "z", "z", "z", "z"),
value= c(7L, 2L, 89L, 2L, 6L)),
.Names= c("Date", "product", "product2", "value"),
class= "data.frame", row.names=c(NA, -5L))
答案 0 :(得分:1)
library(data.table)
data<-fread("Date product product2 value
03/03/2011 a z 7
03/03/2011 a z 2
03/05/2015 b z 89
03/01/2017 a z 2
03/03/2017 c z 6")
data<-dcast(data,product+product2~Date,value.var="value",fun.aggregate = sum)
data[,unique_id:=paste(product,product2,sep="")]
data
product product2 03/01/2017 03/03/2011 03/03/2017 03/05/2015 unique_id
1: a z 2 9 0 0 az
2: b z 0 0 0 89 bz
3: c z 0 0 6 0 cz