Search code examples
rtransactionssequencearules

Turning ContactID and ActivityType dataframe to transaction class


This is my dataframe:

transtest<-structure(list(ContactID = c("003E0000008S8TcIAK", "003E0000008S8U3IAK", 
"003E0000008S8U3IAK", "003E0000008S8U4IAK", "003E0000008S8U4IAK", 
"003E0000008S8U4IAK", "003E0000008S8U4IAK", "003E0000008S8U4IAK", 
"003E0000008S8U4IAK", "003E0000008S8U4IAK"), ActivityType = c("Meeting", 
"LitOrder", "Hard Mail", "LitOrder", "Hard Mail", "Sale", "Inbound", 
"Email", "Outbound", "Left Voicemail/Message with Assistant")), .Names = c("ContactID", 
"ActivityType"), row.names = c(NA, -10L), class = c("tbl_dt", 
"tbl", "data.table", "data.frame"))

I am trying to turn it into a 'transaction' class by doing

read.transactions(transtest, format = "single")

It keeps giving me the following error:

Error in read.transactions(transtest, format = "single") : 
  'cols' must be a numeric or character vector of length 2 for 'single'.

I dont know why this is happening even though length(colnames(transtest)) = 2. Your help is sincerely appreciated!


Solution

  • Folowing the example 4 in arules documentation ?"transactions-class":

    trans1 <- as(split(transtest[,"ActivityType"], transtest[,"ContactID"]), "transactions")
    
    inspect(trans1)
    # items                                          transactionID
    # 1 {Meeting}                               003E0000008S8TcIAK
    # 2 {Hard Mail,                                               
    #   LitOrder}                               003E0000008S8U3IAK
    # 3 {Email,                                                   
    #   Hard Mail,                                               
    #   Inbound,                                                 
    #   Left Voicemail/Message with Assistant,                   
    #   LitOrder,                                                
    #   Outbound,                                                
    #   Sale}                                   003E0000008S8U4IAK