A 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']]]
}
要完成你initital問題,您可以提取DTb
相應列的矩陣
A <- DTb[,-1,with = FALSE]
results <- crossprod(A)
多少「BorS」的級別有(是B還是S?) – mnel 2013-03-15 02:17:09
您是否嘗試過使用稀疏矩陣(Matrix包)? – ndoogan 2013-03-15 02:38:14
是的只有兩個級別B和S.不,我沒有嘗試過稀疏矩陣,現在我會閱讀它。 – user2171177 2013-03-15 03:18:49