Search code examples
raprioriarules

convert character vector to transactions for arules


Please help with converting a character vector of shopping items into "transactions" for arules. The original data is something like:

shopping_items <- c("apple banana", "orange", "tea orange beef")

Each element of the vector represents items bought in a single transaction, and the items are separated by a space " ", for example transaction 1 includes two items which are apple and banana. How can I convert this into "transactions" type so that I can work with it in arules?

Thank you in advance !


Solution

  • This is the short version:

    library(arules)
    shopping_items <- c("apple banana", "orange", "tea orange beef")    
    
    trans <- as(strsplit(shopping_items, " "), "transactions")
    
    inspect(trans)
        items            
    [1] {apple,banana}   
    [2] {orange}         
    [3] {beef,orange,tea}