Changing data to Transactions Type data for running Apriori Algorithm

Hi guys!

I am having troubles changing my basket data to transaction type data, I've tried the following 4 codes:

tr = as(dummy.basket$baskets, "transactions")
Error in as(dummy.basket$baskets, "transactions") :
no method or default for coercing “character” to “transactions”

tr = as(dummy.basket, "transactions")
Warning message:
Column(s) 1 not logical or factor. Applying default discretization (see '? discretizeDF').

tr <- read.transactions(dummy.basket$baskets, format= 'basket', sep = ',')
Error in file(file, "r") : invalid 'description' argument

tr <- read.transactions(dummy.basket, format= 'basket', sep = ',')
Error in readLines(file, encoding = encoding) : 'con' is not a connection

This is the summary for the file:

summary (dummy.basket)
baskets
Length:13683
Class :character
Mode :character

And these are the packages I have:
library(tidyverse)
library(readxl)
library(knitr)
library(ggplot2)
library(lubridate)
library(arules)
library(arulesViz)
library(plyr)

Appreciate any help, thank you!!

There isn't a method for as() called "transactions"

showMethods("coerce")
Function: coerce (package methods)
from="ANY", to="array"
from="ANY", to="call"
from="ANY", to="character"
from="ANY", to="complex"
from="ANY", to="double"
from="ANY", to="environment"
from="ANY", to="expression"
from="ANY", to="function"
from="ANY", to="integer"
from="ANY", to="list"
from="ANY", to="logical"
from="ANY", to="matrix"
from="ANY", to="name"
from="ANY", to="NULL"
from="ANY", to="numeric"
from="ANY", to="S3"
from="ANY", to="S4"
from="ANY", to="ts"
from="ANY", to="vector"
from="character", to="IDate"
from="character", to="ITime"
from="character", to="signature"
from="character", to="SuperClassMethod"
from="color", to="HLS"
from="color", to="HSV"
from="color", to="LAB"
from="color", to="LUV"
from="color", to="polarLAB"
from="color", to="polarLUV"
from="color", to="RGB"
from="color", to="sRGB"
from="color", to="XYZ"
from="data.frame", to="data.table"
from="data.table", to="data.frame"
from="derivedDefaultMethod", to="function"
from="function", to="classGeneratorFunction"
from="function", to="OptionalFunction"
from="groupGenericFunction", to="genericFunction"
from="NULL", to="OptionalFunction"
from="NULL", to="optionalMethod"
from="oldClass", to="S3"

Hi Ven,

Thank you for your response. I think it's part of the arules package? If there isn't a method for as() "transactions. Do you know how I can change my data into transaction-type data? Thank you!

Transforming data into transactions | R for Data Science Cookbook (packtpub.com)

Unfortunately, it's difficult to understand what or where the problem is without a reproducible example.

Page 65 of the arules reference manual only mentions as(x, "transactions") once

The link you provided also produced the same error as you experienced;

 tr_list <- list(c("Apple", "Bread", "Cake"),
                c("Apple", "Bread", "Milk"),
                c("Bread", "Cake", "Milk"))

 names(tr_list) <- paste("Tr", c(1:3), sep = "")

 trans <-  as(tr_list, "transactions")
Error in as(tr_list, "transactions") : 
  no method or default for coercing “list” to “transactions”

I googled the error you received;

I have tried the following, for your first 4 lines of data and worked well for me.

First, from your excel file remove the first line which contains the title "baskets".
then save your excel file as "dummy.basket.csv" to your working directory.

then you will need 3 libraries for the apriori algorithm

library(arules)
library(arulesViz)
library(RColorBrewer)

dummy.basket <- read.transactions("dummy.basket.csv")

dummy.basket
# 
# > dummy.basket
# transactions in sparse format with
# 5 transactions (rows) and
# 7 items (columns)

str(dummy.basket)

# Formal class 'transactions' [package "arules"] with 3 slots
# ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
# .. .. ..@ i       : int [1:9] 1 0 5 2 4 3 6 2 6
# .. .. ..@ p       : int [1:6] 0 1 3 5 7 9
# .. .. ..@ Dim     : int [1:2] 7 5
# .. .. ..@ Dimnames:List of 2
# .. .. .. ..$ : NULL
# .. .. .. ..$ : NULL
# .. .. ..@ factors : list()
# ..@ itemInfo   :'data.frame':	7 obs. of  1 variable:
#   .. ..$ labels: chr [1:7] "Bags," "baskets" "Belts" "MBG" ...
# ..@ itemsetInfo:'data.frame':	0 obs. of  0 variables

Hi Melih,

Thank you!

I just tried it and I am getting the below instead of what you are getting! :frowning:
"Error in readLines(file, encoding = encoding) : 'con' is not a connection"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.