set.seed(1234)
## Generate a logical matrix with 5000 random itemsets for 20 items
m <- matrix(runif(5000 * 20) > 0.8, ncol = 20,
dimnames = list(NULL, paste("item", c(1:20), sep = "")))
head(m)
## Coerce the logical matrix into an itemMatrix object
imatrix <- as(m, "itemMatrix")
imatrix
## An itemMatrix contains a set of itemsets (each row is an itemset).
## The length of the set is the number of rows.
length(imatrix)
## The sparese matrix also has regular matrix dimensions.
dim(imatrix)
nrow(imatrix)
ncol(imatrix)
## Subsetting: Get first 5 elements (rows) of the itemMatrix. This can be done in
## several ways.
imatrix[1:5] ### get elements 1:5
imatrix[1:5, ] ### Matrix subsetting for rows 1:5
head(imatrix, n = 5) ### head()
## Get first 5 elements (rows) of the itemMatrix as list.
as(imatrix[1:5], "list")
## Get first 5 elements (rows) of the itemMatrix as matrix.
as(imatrix[1:5], "matrix")
## Get first 5 elements (rows) of the itemMatrix as sparse ngCMatrix.
## **Warning:** For efficiency reasons, the ngCMatrix is transposed! You
## can transpose it again to get the expected format.
as(imatrix[1:5], "ngCMatrix")
t(as(imatrix[1:5], "ngCMatrix"))
## Get labels for the first 5 itemsets (first default and then with
## custom formating)
labels(imatrix[1:5])
labels(imatrix[1:5], itemSep = " + ", setStart = "", setEnd = "")
## Create itemsets manually from an itemMatrix. Itemsets contain items in the form of
## an itemMatrix and additional quality measures (not supplied in the example).
is <- new("itemsets", items = imatrix)
is
inspect(head(is, n = 3))
## Create rules manually. I use imatrix[4:6] for the lhs of the rules and
## imatrix[1:3] for the rhs. Rhs and lhs cannot share items so I use
## itemSetdiff here. I also assign missing values for the quality measures support
## and confidence.
rules <- new("rules",
lhs = itemSetdiff(imatrix[4:6], imatrix[1:3]),
rhs = imatrix[1:3],
quality = data.frame(support = c(NA, NA, NA),
confidence = c(NA, NA, NA)
))
rules
inspect(rules)
## Manually create a itemMatrix with an item encoding that matches imatrix (20 items in order
## item1, item2, ..., item20)
itemset_list <- list(c("item1","item2"),
c("item3"))
imatrix_new <- encode(itemset_list, itemLabels = imatrix)
imatrix_new
compatible(imatrix_new, imatrix)
Run the code above in your browser using DataLab