transactions-class {arules}R Documentation

Class “transactions” — Binary Incidence Matrix for Transactions

Description

The transactions class represents transaction data used for mining itemsets or rules. It is a direct extension of class itemMatrix to store a binary incidence matrix, item labels, and optionally transaction IDs and user IDs.

Objects from the Class

Objects are created by coercion from objects of other classes (see Examples section) or by calls of the form new("transactions", ...).

Slots

transactionInfo:

a data.frame with vectors of the same length as the number of transactions. Each vector can hold additional information, e.g., store transaction IDs or user IDs for each transaction.

data:

object of class ngCMatrix to store the binary incidence matrix (see itemMatrix class)

itemInfo:

a data.frame to store item labels (see itemMatrix class)

Extends

Class itemMatrix, directly.

Methods

coerce

signature(from = "matrix", to = "transactions"); produces a transactions data set from a binary incidence matrix. The row names are used as item labels and the column names are stores as transaction IDs.

coerce

signature(from = "transactions", to = "matrix"); coerces the transactions data set into a binary incidence matrix.

coerce

signature(from = "list", to = "transactions"); produces a transactions data set from a list. The names of the items in the list are used as item labels and the item IDs and the incidence matrix is produced automatically.

coerce

signature(from = "transactions", to = "list"); coerces the transactions data set into a list of transactions. Each transaction is a vector of character strings (names of the contained items).

coerce

signature(from = "data.frame", to = "transactions"); recodes the data frame containing only categorical variables (factors) or logicals all into a binary transaction data set. For binary variables only TRUE values are converted into items and the item label is the variable name. For factors, a dummy item for each level is automatically generated. Item labels are generated by concatenating variable names and levels with ‘⁠"="⁠’. The original variable names and levels are stored in the itemInfo data frame as the components variables and levels. Note that NAs are ignored (i.e., do not generate an item).

coerce

signature(from = "transactions", to = "data.frame"); represents the set of transactions in a printable form as a data.frame. Note that this does not reverse coercion from data.frame to transactions.

coerce

signature(from = "ngCMatrix", to = "transactions"); Note that the ngCMatrix needs to have the items as rows!

labels

signature(x = "transactions"); returns the labels (item labels and transaction IDs) for the incidence matrix as a list of two vectors named items and transactionID.

transactionInfo<-

signature(x = "transactions"); replaces the transactionInfo data frame

transactionInfo

signature(x = "transactions"); returns transactionInfo

show

signature(object = "transactions")

summary

signature(object = "transactions")

Author(s)

Michael Hahsler

See Also

[-methods, discretize, LIST, write, c, image, inspect, read.transactions, random.transactions, sets, itemMatrix-class

Examples

## example 1: creating transactions form a list
a_list <- list(
      c("a","b","c"),
      c("a","b"),
      c("a","b","d"),
      c("c","e"),
      c("a","b","d","e")
      )

## set transaction names
names(a_list) <- paste("Tr",c(1:5), sep = "")
a_list

## coerce into transactions
trans1 <- as(a_list, "transactions")

## analyze transactions
summary(trans1)
image(trans1)

## example 2: creating transactions from a matrix
a_matrix <- matrix(c(
  1,1,1,0,0,
	1,1,0,0,0,
	1,1,0,1,0,
	0,0,1,0,1,
	1,1,0,1,1
  ), ncol = 5)

## set dim names
dimnames(a_matrix) <- list(c("a","b","c","d","e"),
	paste("Tr",c(1:5), sep = ""))

a_matrix

## coerce
trans2 <- as(a_matrix, "transactions")
trans2
inspect(trans2)

## example 3: creating transactions from data.frame
a_df <- data.frame(
	age   = as.factor(c(6, 8, NA, 9, 16)), 
	grade = as.factor(c("A", "C", "F", NA, "C")),
  pass  = c(TRUE, TRUE, FALSE, TRUE, TRUE))  
## note: factors are translated differently to logicals and NAs are ignored
a_df

## coerce
trans3 <- as(a_df, "transactions") 
inspect(trans3)
as(trans3, "data.frame")

## example 4: creating transactions from a data.frame with 
## transaction IDs and items 
a_df3 <- data.frame(
  TID = c(1,1,2,2,2,3), 
  item=c("a","b","a","b","c", "b")
  )
a_df3
trans4 <- as(split(a_df3[,"item"], a_df3[,"TID"]), "transactions")
trans4
inspect(trans4)

[Package arules version 1.1-7 Index]