Search code examples
rtransactionsdata-miningarules

itemFrequency of two items together


Assume that I have following transactions:

B C A F H
F E C H
E D B
A C H F 
E F A
D H B
E C F B D 
A H C E 
G A E
B H E

I read transactions in R with read.transactions function of arules library. I need item frequency of a specific items. For example for "A".

I can do this with following R code:

itemFrequency(transactions)["A"]

which gives frequency of A. Now, I want frequency of e.g "A" and "C" together and then "A" and not "C". How can I do these with item frequency funcion (It can be any other function, but I need one number (frequency) as an output)


Solution

  • This is tricky!

    # add !C (complement of C)
    > transactions <- addComplement(transactions, labels = "C")
    
    # manually create itemsets {A,C} and {A,!C}
    > itemsets <- encode(list(c("A", "C"), c("A", "!C")), itemLabels(transactions))
    > inspect(itemsets)
       items 
    1 {A,C} 
    2 {A,!C}
    
    # calculate support
    > support(itemsets, transactions)
    [1] 0.2727273 0.1818182