buildMatrix: Move to and from Ratings Matrix

View source: R/ToFromRatingsMat.R

toUserItemRatings,toRatingsMatrixR Documentation

Move to and from Ratings Matrix

Description

Convert to and from the (userID, itemID, rating) data and ratings matrix.

Usage

buildMatrix(ratingsIn)
toRatingsMatrix(ratingsIn, NAval = NA)
toUserItemRatings(ratMat)

Arguments

ratingsIn

Raw data, in (user ID, item ID, rating) format. IDs must be R factors.

ratMat

Ratings matrix

NAval

value to place in the output matrix for missings.

Details

The toRatingsMatrix function inputs a user ratings matrix, and returns the ratings matrix, with element (i,j) containing the rating of item j by user i. The toUserItemRatings function goes the opposite way, converting a ratings matrix to an input data frame.

Author(s)

Norm Matloff

Examples


# for InstEval data, convert to ratings matrix and back, using row 888
# as a check
ivl <- getInstEval()
ivl <- ivl[,1:3]
ivl[888,]  # (47,548,3)
ivla <- toRatingsMatrix(ivl)
ivla['47','548']  # 3
ivls <- toUserItemRatings(ivla)
w <- which(ivls$userID == '47' & ivls$itemID == '548')  #  59321
ivls[59321,]  # (47,548,3)

# HouseVoting data is already in ratings matrix form; convert to input
# data form, using case 888 as a check
hv <- getHouseVoting()
hv <- hv[,-17]  
hv[8,]  # voted no, yes, no, yes on Bills 1-4, didn't vote on Bill 15
hvs <- toUserItemRatings(hv)
w <- which(hvs$userID == '8' & hvs$itemID == 'Bill1')
hvs[w,]  # (8, Bill1, 0)
w <- which(hvs$userID == '8' & hvs$itemID == 'Bill2')
hvs[w,]  # (8, Bill2, 1)
w <- which(hvs$userID == '8' & hvs$itemID == 'Bill15')
w  # empty, didn't vote


matloff/rectools documentation built on March 31, 2022, 12:09 p.m.