我有一个这样的交易数据集: transaction data
我使用“arules”包将其转换为交易数据。
transactions <- read.csv("transactions.csv",header=TRUE,sep=",")
agg <- split(transactions$item,transactions$TransID)
ready <- as(agg,"transactions")
现在我需要一个桌面上的csv文件,它是一个二进制矩阵形式:行中的事务和列中的项目:矩阵中的0和1值如下:binary matrix format
我在“ arules ”包中使用了 write 函数,但是没有。
答案 0 :(得分:0)
您的数据
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
使用表函数,ifelse用于转换二进制矩阵中的表
tab = table(transID,item)
tab
binarymat = ifelse(tab > 0,1,0)
print(binarymat)
# use write.csv function
write.csv(binarymat,"binarymat.csv")