R:将具有相同列名的矩阵中的列组合在一起

时间:2017-08-22 02:55:28

标签: r matrix aggregate

我有一个大型文档 - 术语矩阵。

我想组合具有相同列名的列。

我发现有人问同样的问题并且有工作解决方案。 Combine columns in matrix having same column name

我尝试过使用这个答案。

但是R向我返回错误信息。

  

错误:无法分配大小为49.3 Gb的矢量

#my large matrix
tf_mat <- as.matrix(dtm_title) %>%
          cbind(as.matrix(dtm_abstract)) %>%
          cbind(as.matrix(dtm_claim)) 

#Combine columns with same column name.
nms <- colnames(tf_mat)
tf_mat %*% sapply(unique(nms),"==", nms)

Error: cannot allocate vector of size 49.3 Gb

有什么办法可以解决吗?或者还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

感谢您的所有意见。

我尝试过使用Combine columns in matrix having same column name的另一种方法。

我认为它更适合我的情况,并且不需要花费大量时间来执行。

nms <- colnames(tf_mat)
tf_mat_bind <- sapply(unique(nms), function(i)rowSums(tf_mat[, nms==i, drop=FALSE]))