How to reshape data as transactions in R

transactions
transpose

#1

Hello,

I have some data which looks like:

From this I am trying to create a transactions dataset via:

transact <- subset(sales,select = c('product_id','time_id','customer_id'))
transact$transaction_id <- paste(as.character(transact$time_id),as.character(transact$customer_id),sep = ' ')
transaction.id <- unique(transact$transaction_id)
#Create vectors of each transaction:
transaction.1 <- subset(transact,transaction_id == transaction.id[1],select = c('transaction_id','product_id'))
transaction.1_transpose <- as.data.frame(t(transaction.1[,2]))

This gives me:

Is this the right approach?
How do I do this for the whole dataset?


#2

Hi,

it is a question of test how to build such a table. I usually use deployer for this type of work, I shall transform transaction id as a factor and then do a group_by, this will give me data set grouped by translation id.
If you se ply then you can use all the mutate , filter etc…
Hope this help.
Alain


#3

thanks a lot @Lesaffrea,

I did in this way:

transaction.trns <- sqldf('select transaction_id,product_id from transact group by transaction_id')
transactions <- as.data.frame(t(transaction.trns))
#Now use the split function to create transactions data:
lst1 <- split(transaction.trns[,"transaction_id"], transaction.trns[,"product_id"]) 
transactions_data <- as(lst1, "transactions")