我有一个格式如下的大文件,我读为 x
userid,productid,freq
293994,8,3
293994,5,3
949859,2,1
949859,1,1
123234,1,1
123234,3,1
123234,4,1
...
它给出了给定用户购买的产品及其频率。我试图将它变成一个矩阵,该矩阵将所有 productid 作为列,将 userid 作为行,以频率值作为条目。所以预期的输出是
1 2 3 4 5 8
293994 0 0 0 0 3 3
949859 1 1 0 0 0 0
123234 1 0 1 1 0 0
它是一个稀疏矩阵。我试着做
table(x[[1]],x[[2]])
适用于小文件,但超出了一点 table
给出错误Error in table(x[[1]], x[[2]]) :
attempt to make a table with >= 2^31 elements
Execution halted
有没有办法让它发挥作用?我在 R-3.1.0 上,它应该支持 2^51 大小的向量,所以很困惑为什么它不能处理文件大小。我有 40MM 行,总文件大小为 741M。提前致谢
最佳答案
一 data.table
这样做的方法是:
library(data.table)
library(reshape2)
# adjust fun.aggregate as necessary - not very clear what you want from OP
dcast.data.table(your_data_table, userid ~ productid, fill = 0L)
您可以检查这是否适用于您的数据。
关于R生成稀疏矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397542/