Here is a quick solution using data.table
package:
Step1: Create the data.table
library(data.table)
DT <- data.table(
BillN=c('B1','B1','B1','B1','B2','B2','B2','B2','B3','B3','B3','B3'),
Item_Name=c('Prod A','Prod B','Prod C','Prod D','Prod A','Prod B','Prod C','Prod D','Prod A','Prod B','Prod C','Prod D'), # going on to Product(n)
Quantity=c(1,2,1,2,1,2,1,1,1,2,1,1)
)
Step2: Set appropriate key:
setkey(DT,BillN)
Step3: Make sure that the string vector Item_Name
is not a factor
.
DT[,Item_Name := as.character(Item_Name)]
Step4: Perform the operation by key
DT[,list(Item_Name =paste(Item_Name,collapse=" + "),
Quantity=sum(Quantity)),
by=key(DT)]
You can use the result as it is, or collapse each row! Which is simple!!