I have a shopping cart data, which look like the sample dataframe below:
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
I would like to transform it into different format, which will be like:
#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
I have tried
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
...
However, I don't know how to add Quantity to the data frame. Also, in the actual data frame, there are more columns such as Title, price, which I would like to transform into the ideal data frame as well. Is there a way to transform the data into the ideal format?
With data.table:
which gives
Data (since the OP's original data was borked):