我正在尝试遵循以下问题中的工作流程:Converting from regular format to sparse format for arules package。我有一些交易数据,我想把它变成一个稀疏矩阵,可以送入8
包中。发布的问题的解决方案不再起作用。见下文:
> ncol(x)
[1] 8
> length(mn)
[1] 8
至少,我想将上述格式转换为以下格式(然后可以在随后的一些步骤中将其转换为arules
可读对象):
ID <- c(1, 2, 2, 8, 8, 9, 10, 11)
Item <- c("Avas", "Alo", "Erbi", "Abra", "Ali", "Inj", "Avas", "Avas")
(test <- data.frame(ID, Item))
ID Item
1 Avas
2 Alo
2 Erbi
8 Abra
8 Ali
8 Inj
9 Inj
10 Avas
11 Avas
trans1 <- as(split(test[, "Item"], test[, "ID"]), "transactions")
Error in as(split(test[, "Item"], test[, "ID"]), "transactions") :
no method or default for coercing “list” to “transactions”
答案 0 :(得分:1)
read.table(text=do.call(paste,aggregate(.~ID,test,paste,collapse = ' ')),fill=T,h=F)
V1 V2 V3 V4
1 1 Avas
2 2 Alo Erbi
3 8 Abra Ali Inj
4 9 Inj
5 10 Avas
6 11 Avas
您注意确保数据框仅包含字符。如果不是这种情况,请
test = rapply(test,as.character,'factors',how='replace')
您也可以这样做:
reshape(transform(test,time=ave(ID,ID,FUN=seq_along)),idvar = 'ID',dir='wide')
ID Item.1 Item.2 Item.3
1 1 Avas <NA> <NA>
2 2 Alo Erbi <NA>
4 8 Abra Ali Inj
7 9 Inj <NA> <NA>
8 10 Avas <NA> <NA>
9 11 Avas <NA> <NA>