2
我有一個購物車的數據,看起來像下面的示例數據框:收集多列與tidyr
sample_df<-data.frame(
clientid=1:10,
ProductA=c("chair","table","plate","plate","table","chair","table","plate","chair","chair"),
QuantityA=c(1,2,1,1,1,1,2,3,1,2),
ProductB=c("table","doll","shoes","","door","","computer","computer","","plate"),
QuantityB=c(3,1,2,"",2,"",1,1,"",1)
)
#sample data frame
clientid ProductA QuantityA ProductB QuantityB
1 1 chair 1 table 3
2 2 table 2 doll 1
3 3 plate 1 shoes 2
4 4 plate 1
...
10 10 chair 2 plate 1
我想將其轉換成不同的格式,這將是這樣的:
#ideal data frame
clientid ProductNumber Product Quantity
1 1 A chair 1
2 1 B table 3
3 2 A table 2
4 2 B doll 1
...
11 6 A chair 1
...
17 10 A chair 2
18 10 B plate 1
我試圖
library(tidyr)
sample_df_gather<- sample_df %>% select(clientid, ProductA, ProductB)
%>% gather(ProductNumber, value, -clientid) %>% filter(!is.na(value))
#this gives me
clientid ProductNumber value
1 1 ProductA chair
2 2 ProductB table
3 3 ProductA plate
4 4 ProductB plate
...
不過,我不知道該怎麼數量添加到數據幀。另外,在實際的數據框架中,還有更多的欄目,例如標題,價格,我希望將其轉換爲理想的數據框架。有沒有辦法將數據轉換爲理想的格式?
對於QuantityB,你真的不想用「」......試試NA。 – Frank
'reshape(sample_df,dir ='long',vary = list(c(2,4),c(3,5)))'給了我20行或是錯誤的 – rawr
謝謝@Frank!這裏提供的重塑功能解決了我的問題。 @aosmith,是的,在我問這個問題之前,我已經檢查過它,但仍然無法找到一種方法將我轉換爲理想的數據框架。 –