Search code examples
rr-packagearules

How to convert object of Transaction to Dataframe in R


How to convert the Dataset(Groceries) in arules package to dataframe.

class(Groceries)
[1] "transactions"
attr(,"package")
[1] "arules"

Solution

  • You need to specify what is actually needed. I don't see an as.data.frame.transactions function in the help page for the arules-package. The data-item Groceries does have a dataframe embedded in it but whether that is what you want seems unlikely:

    str(Groceries)
    Formal class 'transactions' [package "arules"] with 3 slots
      ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
      .. .. ..@ i       : int [1:43367] 13 60 69 78 14 29 98 24 15 29 ...
      .. .. ..@ p       : int [1:9836] 0 4 7 8 12 16 21 22 27 28 ...
      .. .. ..@ Dim     : int [1:2] 169 9835
      .. .. ..@ Dimnames:List of 2
      .. .. .. ..$ : NULL
      .. .. .. ..$ : NULL
      .. .. ..@ factors : list()
      ..@ itemInfo   :'data.frame': 169 obs. of  3 variables:
      .. ..$ labels: chr [1:169] "frankfurter" "sausage" "liver loaf" "ham" ...
      .. ..$ level2: Factor w/ 55 levels "baby food","bags",..: 44 44 44 44 44 44 44 42 42 41 ...
      .. ..$ level1: Factor w/ 10 levels "canned food",..: 6 6 6 6 6 6 6 6 6 6 ...
      ..@ itemsetInfo:'data.frame': 0 obs. of  0 variables
    

    I'm guessing you actually want:

    as.matrix( Groceries@data )
    

    Possibly adding the Groceries@ itemInfo$ labels as rownames