Let’s start by constructing a basic sparse matrix where element (i,j) = TRUE if customer i purchased product j, and FALSE otherwise. RowIdx and ColIdx in the orders table will be the (i,j) location of each record in the resulting matrix. # Build a table of customers custs <- ame ( Customer = c ( "Bob", "Sue", "Ralf", "John", "Mary" )) custs # Customer # 1 Bob # 2 Sue # 3 Ralf # 4 John # 5 Mary # Build a table of products prods <- ame ( Product = c ( "AAA", "BBB", "CCC", "DDD", "EEE", "FFF" )) prods # Product # 1 AAA # 2 BBB # 3 CCC # 4 DDD # 5 EEE # 6 FFF # Build a table of orders: (customer, product, units) triplets orders <- ame ( Customer = c ( "Bob", "Bob", "Sue", "Sue", "Bob", "Ralf", "John", "John" ), Product = c ( "AAA", "AAA", "FFF", "FFF", "EEE", "EEE", "AAA", "FFF" ) ) # Insert RowIdx column, identifying the row index to assign each customer orders $ RowIdx <- match ( orders $ Customer, custs $ Customer ) # Insert ColIdx column, identifying the column index to assign each product orders $ ColIdx <- match ( orders $ Product, prods $ Product ) # Inspect orders # Customer Product RowIdx ColIdx # 1 Bob AAA 1 1 # 2 Bob AAA 1 1 # 3 Sue FFF 2 6 # 4 Sue FFF 2 6 # 5 Bob EEE 1 5 # 6 Ralf EEE 3 5 # 7 John AAA 4 1 # 8 John FFF 4 6
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |