0
votes

I have a transaction dataset like this: transaction data

I used "arules" package to transform it to transaction data.

transactions <- read.csv("transactions.csv",header=TRUE,sep=",") agg <- split(transactions$item,transactions$TransID) ready <- as(agg,"transactions")

Now I need a csv file on my desktop which is a binary matrix form: Transactions in the rows and items in the columns: 0 and 1 values in the matrix like this: binary matrix format

I used write function in the "arules" package but could not have it.

1
would table work? - user20650

1 Answers

0
votes

Your data

transID <- c(48864208,48864208,48864266,48864266,48864266,48864266,48864276,48864276,
             48864276,48864276,48864282,48864282,48864282,48864282,48864282,48864282,
             48864282,48864282,48864282,48864296,48864296,48864300,48864300,48864300,
             48864300,48864300,48864300,48864300,48864300,48864300)
item <- c("dinner","cleaning","breakfast","breakfast","drink","dinner","dinner",
          "dinner","dinner","dinner","breakfast","snack","snack","snack","breakfast",
          "dinner","dinner","breakfast","dinner","dinner","dinner","fruit","fruit",
          "fruit","dinner","breakfast","breakfast","drink","cleaning","dinner")
head(data.frame(transID,item))
 transID      item
1 48864208    dinner
2 48864208  cleaning
3 48864266 breakfast
4 48864266 breakfast
5 48864266     drink
6 48864266    dinner

use table function, and ifelse for transform the table in binary matrix

tab = table(transID,item)
tab
binarymat = ifelse(tab > 0,1,0)
print(binarymat)
# use write.csv function
write.csv(binarymat,"binarymat.csv")

enter image description here