简体   繁体   English

根据交易行在R中创建矩阵

[英]Creating a matrix in R from transaction rows

I have transaction data, I need to create an association matrix from it. 我有交易数据,我需要从中创建一个关联矩阵。 I tried self-joining but nothing seems to work. 我尝试过自动加入,但似乎没有任何效果。 Below is the sample code and the desired output. 下面是示例代码和所需的输出。 I am looking for a solution using data table in R 我正在寻找使用R中的数据表的解决方案

> TP <- data.table(
  Tr = c("T1","T1","T2","T2","T2", "T3", "T4"),
  Pr = c("P1","P2","P3","P1","P4", "P2", "P9")
)


> TP
   Tr Pr
1: T1 P1
2: T1 P2
3: T2 P3
4: T2 P1
5: T2 P4
6: T3 P2
7: T4 P9

Desired output is: 所需的输出是:

   T1 T2 T3 T4
1:  1  1  0  0
2:  1  0  1  0
3:  1  0  0  0
4:  0  1  0  0
5:  0  0  0  1

OR even better if possible to get something like this. 甚至更好的办法是得到这样的东西。

   Pr T1 T2 T3 T4
1: P1  1  1  0  0
2: P2  1  0  1  0
3: P3  1  0  0  0
4: P4  0  1  0  0
5: P9  0  0  0  1

This should work: 这应该工作:

dcast(TP, Pr ~ Tr, fun.aggregate = function(x){(length(x) > 0) * 1})

Using 'Pr' as value column. Use 'value.var' to override
   Pr T1 T2 T3 T4
1: P1  1  1  0  0
2: P2  1  0  1  0
3: P3  0  1  0  0
4: P4  0  1  0  0
5: P9  0  0  0  1

@David Arenburg's suggestions is much cleaner if we have no duplicate associations: 如果我们没有重复的关联,则@David Arenburg的建议要干净得多:

dcast(TP, Pr ~ Tr, length)

I would do it with dcast() as suggested by David Arenburg but here is (a fast) an alternative just for fun: 我会按照David Arenburg的建议使用dcast() ,但这是(一种快速的)替代方法,只是为了好玩:

TP[, data.table(unclass(table(Pr, Tr)), keep.rownames = "Pr")]
   Pr T1 T2 T3 T4
1: P1  1  1  0  0
2: P2  1  0  1  0
3: P3  0  1  0  0
4: P4  0  1  0  0
5: P9  0  0  0  1

Benchmark: 基准测试:

It seems that when dealing with millions of rows dcast() is faster: 似乎在处理数百万行时, dcast()更快:

TP1 <- data.table(
  Tr = paste0("T", sample(1:10, size = 1e5, replace = TRUE)),
  Pr = paste0("P", sample(1:1e4, size = 1e5, replace = TRUE))
)

TP_huge <- data.table(
  Tr = paste0("T", sample(1:10, size = 1e7, replace = TRUE)),
  Pr = paste0("P", sample(1:1e4, size = 1e7, replace = TRUE))
)

microbenchmark::microbenchmark(
  table1 = TP1[, data.table(unclass(table(Pr, Tr)), keep.rownames = "Pr")],
  dcast1 = dcast(TP1, Pr ~ Tr, length, value.var = "Pr"),
  table_huge = TP_huge[, data.table(unclass(table(Pr, Tr)), keep.rownames = "Pr")],
  dcast_huge = dcast(TP_huge, Pr ~ Tr, length, value.var = "Pr"),
  times = 5
)
Unit: milliseconds
       expr        min        lq      mean    median        uq       max neval  cld
     table1   92.71867  105.8366  127.4707  124.4188  150.0642  164.3155     5 a   
     dcast1  255.53793  271.5194  292.2005  301.4840  302.5010  329.9600     5  b  
 table_huge 1719.83678 1732.1086 1771.0142 1733.8847 1771.5087 1897.7325     5    d
 dcast_huge  917.94755  927.1657  971.4084  986.1038  998.1780 1027.6468     5   c 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM