I know you can see the content of each transaction using inspect
and then extract the items yourself, but is there a convenient way to get a list of all items which belong to a transaction?
Asked
Active
Viewed 2,685 times
1

Milad
- 63
- 1
- 7
-
1It would be easier to help you with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Maybe you can even include one from the package help pages and make it clear what information you want to extract and how you want to store it. – MrFlick Aug 18 '16 at 01:28
3 Answers
2
a_list <- list( # Create example data
c("a","b","c"),
c("a","b"),
c("a","b","d"),
c("c","e"),
c("a","b","d","e")
)
## set transaction names
names(a_list) <- paste("Tr",c(1:5), sep = "")
a_list
## coerce into transactions
trans1 <- as(a_list, "transactions")
## analyze transactions
summary(trans1)
image(trans1)
# if you want an actual list structure:
trans1 <- as(a_list, "list")
This is the plot of it from image()
:
Now with a bigger transactions
object:
data("Adult")
adult_list <- as(Adult, "list")

Hack-R
- 22,422
- 14
- 75
- 131
2
There is an R package called data.table
that allow you to do this type of transaction aggregations.
For example:
data <- data.frame(
transactionID = c("1001", "1001", "1002", "1003", "1003", "1003"),
item= c("A", "B", "B", "A", "B", "C")
)
One way to get a list of all items which belong to a transaction:
Aggregate Items By Transaction. Data.table
Other way, by the function split
:
data.aggregate <- split(data$item, data$transactionID)
> data.aggregate
$`1001`
[1] A B
Levels: A B C
$`1002`
[1] B
Levels: A B C
$`1003`
[1] A B C
Levels: A B C
0
A possible simple solution if your goal is just to see all of the items contained across all transactions (that is, a complete item list) is the following:
unique(unlist(as(a_list, "list")))