【问题标题】:Turn transactions data into sparse matrix将交易数据转化为稀疏矩阵
【发布时间】:2019-02-20 17:10:52
【问题描述】:

我正在尝试遵循以下问题中的工作流程:Converting from regular format to sparse format for arules package。我有一些交易数据,我想把它变成一个稀疏矩阵,可以输入到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”

至少,我想将上述格式转换为以下格式(然后我可以在后续步骤中将其转换为arules 可读对象):

   V1    V2   V3
1  Avas
2  Alo   Erbi
8  Abra  Ali  Inj
9  Inj
10 Avas
11 Avas

【问题讨论】:

  • 最终产品的形状是什么?如果是稀疏矩阵,那么格式是什么?

标签: r matrix


【解决方案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>

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2021-11-25
    • 2017-07-02
    • 2018-01-19
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2020-12-07
    • 2021-12-14
    相关资源
    最近更新 更多