2017-10-06 70 views
0

假設我有(r1,... rm)行和(c1,c2,... cn)矩陣,所有元素都是0和1。針對不同列的組合計算0和1的數字

我想數0和1的總數爲不同的組合:例如,C1 & C2,C1 & C3,C1 & C3,C1 & C2 & C3,C1 & C3 C4 &。

有沒有一種有效的方法來計算這些?

我這樣做很差,其中數據是我的矩陣。

is.one <- function(data,zero.one) 
{ 
     #zero.one is logical , T, counting 1, otherwise 0s. 

     if (zero.one) 
      return (data==1) 
     else 
      return (data==0) 
} 

sum.one <- function(data, comb, zero.one) 
{ 
     #comb is one of the combinations as a vector 
     index<- rep(T,nrow(data)) 

     for (i in 1: length(comb)) 
     { 
      # assuming i-th column is the i-th element of combination 
      index <- is.one(data[,i], zero.one[i]) 
      data <- data[index,] 
     } 

     return(sum(index)) 
} 

例子:

sum.one (data, c("c1","c2"), c(1,1)) 

sum.one (data, c("c1","c2","c3"), c(1,1,1)) 

sum.one (data, c("c1","c2","c3"), c(1,1,0)) 

我寧願不計算C1或C2他們出現在每個組合,並保持指數可能是內存問題當m(nrow(數據))是大。

任何意見,將不勝感激。

回答

0

我的想法是將矩陣轉換爲使用reshape2

df <- as.data.frame(your_matrix) 

然後你就可以輕鬆地總結出列,並將它們保存在另一個變量

df <- data.frame(
    c1 = sample(c(0, 1), replace = TRUE, size = 100), 
    c2 = sample(c(0, 1), replace = TRUE, size = 100), 
    c3 = sample(c(0, 1), replace = TRUE, size = 100), 
    c4 = sample(c(0, 1), replace = TRUE, size = 100) 
) 

    ones <- as.numeric(colSums(df)) 
    zeros <- as.numeric(NROW(df) - ones) 


> ones 
c1 c2 c3 c4 
39 45 41 50 

> zeros 
c1 c2 c3 c4 
61 55 59 50 

一個數據幀,那麼你可以使用這些矢量爲您的組合。例如:列2中有多少個,列4中有多少個0?

> answer <- as.numeric(ones[2] + zeros[4]) 
> answer 
[1] 95 
+1

任何理由進口'reshape2'? – Bernhard

+0

雖然有效,但我認爲我的功能也可以正常工作,但不能提高內存效率。如果我想知道c1&!c4&c5,那麼我需要從頭開始計算它,但是您知道哪些行是上面的c1&!c4。 –

+0

對不起,reshape2 - 導入沒有必要。我已經刪除它,謝謝指出它。 – brettljausn

0
data <- matrix(c(1, 0, 0, 0, 0, 0, 1, 0, 1), 3, 3) 
rownames(data) <- paste0("r", 1:nrow(data)) 
colnames(data) <- paste0("c", 1:ncol(data)) 
data 
# c1 c2 c3 
# r1 1 0 1 
# r2 0 0 0 
# r3 0 0 1 

你可以創建一個包含所有結果的多維對象,然後選擇您需要的值:

x <- colSums(data) 
y <- colSums(data==0) 
names(y) <- paste0(names(y), "_0") 
o1 <- outer(x, y, FUN = "+") 
o1 
# c1_0 c2_0 c3_0 
# c1 3 4 2 
# c2 2 3 1 
# c3 4 5 3 

o2 <- outer(o1, y, FUN = "+") 
o2 
# , , c1_0 
# 
# c1_0 c2_0 c3_0 
# c1 5 6 4 
# c2 4 5 3 
# c3 6 7 5 
# 
# , , c2_0 
# 
# c1_0 c2_0 c3_0 
# c1 6 7 5 
# c2 5 6 4 
# c3 7 8 6 
# 
# , , c3_0 
# 
# c1_0 c2_0 c3_0 
# c1 4 5 3 
# c2 3 4 2 
# c3 5 6 4 

o2[1, 1, 2] 
# [1] 6 
+0

謝謝,但這對於一個大矩陣是不實際的,當n&m大於100000時。 –

+0

在你的例子中:c1&c2 = 0,c1&c3 = 1,!c1&!c2 = 2。你的迴應並沒有給我我想要的。我想有一個保持以前計算的函數,所以它不會每一次都這樣做。 –