0

First we have a transaction data, we can use the built in dataset.

require(arules)

## Can use built-in example dataset
require(datasets)
data(Groceries)

groceries <- as ( "transactions") # convert to 'transactions' class

summary(groceries)

And the output is:

most frequent items:
  whole milk other vegetables       rolls/buns             soda           yogurt          (Other) 
        2513             1903             1809             1715             1372            34055 

But then we have another data table that we want to data use for labeling:

itemnum <- c(1,2,3,4,5)
ProductName_ <- factor(c("whole milk", "other vegetables", "rolls/buns", "soda", "yogurt"))
ProductNames <- data.frame(itemnum, ProductName_)

How can I replace the product description on the first table with the itemnum from the second?

So when I run:

summary(groceries)

The output is:

most frequent items:
     1      2       3      4       5       (Other) 
  2513   1903    1809   1715    1372         34055 
jpf5046
  • 729
  • 7
  • 32
  • Welcome to StackOverflow. Please read [how do I ask a good question](http://stackoverflow.com/help/how-to-ask), [How to create a MCVE](http://stackoverflow.com/help/mcve) as well as [how to provide a minimal reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit and improve your question accordingly. I.e., provide the packages needed (e.g. `library(arules)`), input data (e.g. the result of `dput(groctrans)`, the expected output, what lines of code you tried and in what they failed. – lukeA Apr 22 '16 at 21:07
  • Edited, thanks Luke. – jpf5046 Apr 22 '16 at 22:07

1 Answers1

0

you can change the data itself before calling summary

library(arules)
library(datasets)
data(Groceries)
summary(Groceries)
# transactions as itemMatrix in sparse format with
# 9835 rows (elements/itemsets/transactions) and
# 169 columns (items) and a density of 0.02609146 
# 
# most frequent items:
#     whole milk other vegetables       rolls/buns             soda           yogurt 
# 2513             1903             1809             1715             1372 
# (Other) 
# 34055 
# 
# element (itemset/transaction) length distribution:
#     sizes
# 1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 
# 2159 1643 1299 1005  855  645  545  438  350  246  182  117   78   77   55   46   29   14   14 
# 20   21   22   23   24   26   27   28   29   32 
# 9   11    4    6    1    1    1    1    3    1 
# 
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.000   2.000   3.000   4.409   6.000  32.000 
# 
# includes extended item information - examples:
#     labels  level2           level1
# 1 frankfurter sausage meet and sausage
# 2     sausage sausage meet and sausage
# 3  liver loaf sausage meet and sausage


itemnum <- c(1,2,3,4,5)
ProductName_ <- factor(c("whole milk", "other vegetables", "rolls/buns", "soda", "yogurt"))
ProductNames <- data.frame(itemnum, ProductName_)

#change values in Groceries@itemInfo$labels check out plyr::mapvalues as well
Groceries@itemInfo$labels <- ProductNames$itemnum[match(Groceries@itemInfo$labels,ProductNames$ProductName_)]
summary(Groceries)
# transactions as itemMatrix in sparse format with
# 9835 rows (elements/itemsets/transactions) and
# 169 columns (items) and a density of 0.02609146 
# 
# most frequent items:
#     1       2       3       4       5 (Other) 
# 2513    1903    1809    1715    1372   34055 
# 
# element (itemset/transaction) length distribution:
#     sizes
# 1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 
# 2159 1643 1299 1005  855  645  545  438  350  246  182  117   78   77   55   46   29   14   14 
# 20   21   22   23   24   26   27   28   29   32 
# 9   11    4    6    1    1    1    1    3    1 
# 
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.000   2.000   3.000   4.409   6.000  32.000 
# 
# includes extended item information - examples:
#     labels  level2           level1
# 1     NA sausage meet and sausage
# 2     NA sausage meet and sausage
# 3     NA sausage meet and sausage
chinsoon12
  • 25,005
  • 4
  • 25
  • 35