我正在进行数据帧转换,并且正在上一篇文章中使用Arun和Ricardo
Arun,提出了一个出色的解决方案(矩阵乘法)来实现我想要做的事情。该解决方案适用于我在示例中提到的小数据集,现在我在具有以下大小的数据框架上运行相同的解决方案:
Total rows: 143345
Total Persons: 98461
Total Items : 30
现在,当我运行以下命令时
A <- acast(Person~Item+BorS,data=df,fun.aggregate=length,drop=FALSE)
我收到此错误..
Error: segfault from C stack overflow
这是因为,我没有足够的处理/内存功率。我的机器有4 GB RAM,2.8 GHz i7处理器(Macbook)?我们如何处理这类案件?
答案 0 :(得分:4)
data.table
解决方案。这通过首先聚合,然后创建新的data.table并通过引用填写
library(data.table)
# some sample data
DT <- data.table(Person = sample(98461, 144000, replace = TRUE), item = sample(c(letters,LETTERS[1:4]), 144000, replace = TRUE), BorS = sample(c('B','S'), 144000, replace = TRUE))
# aggregate to get the number of rows in each subgroup by list item and BorS
# the `length` of each subgroup
DTl <- DT[,.N , by = list(Person, item, BorS)]
# the columns you want to create
newn <- sort(DT[, do.call(paste0,do.call(expand.grid,list(unique(item),unique(BorS) )))])
# create a column which has this id combination in DTl
DTl[, comnb := paste0(item, BorS)]
# set the key so we can join / subset easily
setkey(DTl, comnb)
# create a data.table that has 1 row for each person, and has columns for all the combinations
# of item and BorS
DTb <- DTl[, list(Person)][, c(newn) := 0L]
# set the key so we can join / subset easily
setkey(DTb, Person)
# this bit could be far quicker, but I think
# would require a feature request to data.table
for(nn in newn){
# for each of the cominations extract which persons have
# this combination >0
pp <- DTl[list(nn), list(Person,N)]
# for the people who have N > 0
# assign the correct numbers in the correct column in DTb
DTb[list(pp[['Person']]), c(nn) := pp[['N']]]
}
要完成初始问题,您可以从DTb
中提取相应的列作为矩阵
A <- DTb[,-1,with = FALSE]
results <- crossprod(A)