binaryRatingMatrix | R Documentation |
A matrix to represent binary rating data. 1 codes for a positive rating and 0 codes for either no or a negative rating. This coding is common for market basked data where products are either bought or not.
Objects can be created by calls of the form new("binaryRatingMatrix", data = im)
, where im
is an itemMatrix
as defined in package
arules, by coercion from a matrix (all non-zero values will be a 1),
or by using binarize
for
an object of class "realRatingMatrix".
data
:Object of class "itemMatrix"
(see package arules)
Class "ratingMatrix"
, directly.
signature(from = "matrix", to = "binaryRatingMatrix")
:
The matrix needs to be a logical matrix, or a 0-1 matrix (0 means FALSE and 1 means TRUE).
NAs are interpreted as FALSE.
signature(from = "itemMatrix", to = "binaryRatingMatrix")
signature(from = "data.frame", to = "binaryRatingMatrix")
signature(from = "binaryRatingMatrix", to = "matrix")
signature(from = "binaryRatingMatrix", to = "dgTMatrix")
signature(from = "binaryRatingMatrix", to = "ngCMatrix")
signature(from = "binaryRatingMatrix", to = "dgCMatrix")
signature(from = "binaryRatingMatrix", to = "itemMatrix")
signature(from = "binaryRatingMatrix", to = "list")
itemMatrix
in arules,
getList
.
## create a 0-1 matrix
m <- matrix(sample(c(0,1), 50, replace=TRUE), nrow=5, ncol=10,
dimnames=list(users=paste("u", 1:5, sep=''),
items=paste("i", 1:10, sep='')))
m
## coerce it into a binaryRatingMatrix
b <- as(m, "binaryRatingMatrix")
b
## coerce it back to see if it worked
as(b, "matrix")
## use some methods defined in ratingMatrix
dim(b)
dimnames(b)
## counts
rowCounts(b) ## number of ratings per user
colCounts(b) ## number of ratings per item
## plot
image(b)
## sample and subset
sample(b,2)
b[1:2,1:5]
## coercion
as(b, "list")
head(as(b, "data.frame"))
head(getData.frame(b, ratings=FALSE))
## creation from user/item tuples
df <- data.frame(user=c(1,1,2,2,2,3), items=c(1,4,1,2,3,5))
df
b2 <- as(df, "binaryRatingMatrix")
b2
as(b2, "matrix")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.