我有命名爲數據的數據幀,其具有以下要素:數據分類
Model Garage City Unit.Price Invoice.Date Components
Hyundai A NY 500 31/12/2016 HL
Honda B NJ 700 31/12/2016 TL
Porsche A NY 800 30/12/2016 TL
BMW B NJ 800 30/12/2016 HL
BMW A NJ 700 31/12/2016 HL
Porsche B NY 800 30/12/2016 TL
Honda A NY 400 30/12/2016 TL
Honda A NY 500 30/12/2016 HL
Honda B NY 600 30/12/2016 HL
Honda A NY 200 29/12/2016 TL
Honda A NY 300 29/12/2016 HL
我想數據的輸出分成汽車排序與Invoice.Date
使得電流成本第一捕獲。
Ex:Honda
Components GarageA GarageB
HL 500 600
TL 400 700
這是我如何開始:
Category <- as.data.frame(c("BMW","Honda","Porsche","Hyundai"))
for(i in 1:nrow(Category))
{
m <- Category[i,1]
X <- subset(Data,Model==m)
X <- Data[order(Data$Invoice.Date,decreasing = T),]
Pivot_A<-dcast(X,Name~Garage,value.var = "Unit.Price",function(x) length((x)))
write.csv(Pivot,file = paste(X,"Cars.csv",sep = "_"))
}
我得到的唯一問題是映射正確的單價。有沒有任何代碼或功能與dcast
做到這一點? dcast
有sum
,count
的選項。如果我想要的確切金額,而不是sum
,average
。