2016-07-08 64 views
1

我有一個數據框,包含過去12個月的庫存數據。我在下面創建了三個月的模擬數據框,這與我的數據集相似。整形數據幀的整形

inventory <- data.frame(ID=c(1,1,1,1,2,2,3,3,3,3,4,4,4), 
         SKU=c("375F","375F","375F","375F","QX51","QX51","AEC","AEC","AEC","AEC","115332H","115332H","115332H"), 
         inventory=c(3,4,14,5,18,5,4,13,4,10,3,2,2), 
         sold=c(3,2,0,1,4,0,0,3,1,5,0,2,1), 
         returned=c(1,0,2,0,0,0,1,0,1,1,0,2,0), 
         month=c(0,1,2,3,0,2,3,0,1,2,3,2,3)) 

我試圖操縱數據幀以產生顯示每個變量與它們的ID和SKU和每個月一列,像下面的圖像的報告。

重塑的數據框我嘗試過使用dplyr和data.table庫,但沒有取得任何成功。如何將數據轉換爲每個月都有一列,就像我發佈的圖片一樣? R對我來說還很新,所以放在我身上很容易。謝謝。

enter image description here

+1

你應該在問題中包含你的圖片。插入鏈接到圖片是一個壞主意,因爲最終這些鏈接變得不可用,未來的用戶將無法從您的問題中受益。你可以編輯你的帖子來解決這個問題。 – Andrej

+0

謝謝你的提示,Andrej。更新。 –

+0

更好的是,不要使用圖像。有更好的方法來顯示預期的結果。 – Sotos

回答

4

沒有爲ID = 4 and SKU = 115332H的複製,所以我不得不改變的值刪除重複。

# Creating the data frame 
inventory <- data.frame(ID=c(1,1,1,1,2,2,3,3,3,3,4,4,4), 
         SKU=c("375F","375F","375F","375F","QX51","QX51","AEC","AEC","AEC","AEC","115332H","115332H","115332H"), 
         inventory=c(3,4,14,5,18,5,4,13,4,10,3,2,2), sold=c(3,2,0,1,4,0,0,3,1,5,0,2,1), 
         returned=c(1,0,2,0,0,0,1,0,1,1,0,2,0), 
         month=c(0,1,2,3,0,2,3,0,1,2,1,2,3)) 

# Reshaping the data 
    # Melting the data frame 
    inv2 <- melt(inventory,id=c("ID","SKU","month")) 
    # Reshaping 
    inv2_wide <- reshape(inv2,v.names = "value",idvar = c("ID","SKU","variable"), 
         timevar = "month", direction = "wide") 

# Ordering by ID variables 
inv2_wide <- inv2_wide[order(inv2_wide$ID,inv2_wide$SKU),] 

# Renaming the variables 
names(inv2_wide) <- gsub("value\\.","Month",names(inv2_wide)) 


    ID  SKU variable Month0 Month1 Month2 Month3 
1 1 375F inventory  3  4  14  5 
14 1 375F  sold  3  2  0  1 
27 1 375F returned  1  0  2  0 
5 2 QX51 inventory  18  NA  5  NA 
18 2 QX51  sold  4  NA  0  NA 
31 2 QX51 returned  0  NA  0  NA 
7 3  AEC inventory  13  4  10  4 
20 3  AEC  sold  3  1  5  0 
33 3  AEC returned  0  1  1  1 
11 4 115332H inventory  NA  3  2  2 
24 4 115332H  sold  NA  0  2  1 
37 4 115332H returned  NA  0  2  0 
1

我們可以使用tidyr

library(dplyr) 
library(tidyr) 
gather(inventory, Variable,value, inventory:returned) %>% #reshape to long 
     mutate(month = paste0("Month", month)) %>% #concat with "Month" string 
     spread(month, value)#reshape to wide 
# ID  SKU Variable Month0 Month1 Month2 Month3 
#1 1 375F inventory  3  4  14  5 
#2 1 375F returned  1  0  2  0 
#3 1 375F  sold  3  2  0  1 
#4 2 QX51 inventory  18  NA  5  NA 
#5 2 QX51 returned  0  NA  0  NA 
#6 2 QX51  sold  4  NA  0  NA 
#7 3  AEC inventory  13  4  10  4 
#8 3  AEC returned  0  1  1  1 
#9 3  AEC  sold  3  1  5  0 
#10 4 115332H inventory  NA  3  2  2 
#11 4 115332H returned  NA  0  2  0 
#12 4 115332H  sold  NA  0  2  1