2017-08-16 68 views
1

這裏我有一個5 * 4的矩陣(原來大得多)。我想計算矩陣每一行中每對唯一對的比率。計算矩陣中每一行唯一對的劃分

X1 X2 X3 X4 
10 8 2 1 
4 4 3 6 
2 10 8 1 
1 2 1 10 
3 5 5 4 

我想實現與非重複分裂5 * 6矩陣,如下所示>

x1/x2 x1/x3 x1/x4 x2/x3 x2x4 x3/x4 
1.25 5.00 10.00 4.00 8.00 2.00 
1.00 1.33 0.67 1.33 0.67 0.50 
0.20 0.25 2.00 1.25 10.00 8.00 
0.50 1.00 0.10 2.00 0.20 0.10 
0.60 0.60 0.75 1.00 1.25 1.25 

現在我已經創建了我希望會做的伎倆一個功能,但是結果並不如預期。

set.seed(7) 
test <- data.frame(replicate(4,sample(1:10,5,rep=TRUE))) 

func_calcRatio <- function(theMatrix){ 

    ratios <- outer(theMatrix, theMatrix, '/') 
    ratios <- ratios[upper.tri(ratios)] 
    return(ratios) 
} 


func_ratioMatrix <- function(theMatrix){ 

    ratios_list <- list() 
    i = 1 
    l = length(theMatrix) 
    for (i in 1:l) { 
    vec <- numeric(l) 
    for (j in 1:l){ 
     vec[j] <- i^j 
    } 
    myrow <- theMatrix[,1] 
    onerow <- func_calcRatio(myrow) 
    ratios_list[[i]] <- onerow 
    i = i+1 
    } 

    ratios_df <- do.call("rbind", ratios_list) 
    return(ratios_df) 
} 

test.ratios <- func_ratioMatrix(test) 
+0

要注意,從打印輸出,這看起來很像一個數據框,而不是一個矩陣。這是R中的一個重要區別,因爲這些不同的對象類型通常使用不同的解決方案。你可以使用'class(objectName)'在這個實例中告訴對象類型。 – lmo

回答

3

讓上面的矩陣爲A。然後就可以用下面的代碼:

combn(4,2,function(x) A[,x[1]]/A[,x[2]]) 
     [,1]  [,2]  [,3]  [,4]  [,5] [,6] 
    [1,] 1.25 5.000000 10.0000000 4.000000 8.0000000 2.00 
    [2,] 1.00 1.333333 0.6666667 1.333333 0.6666667 0.50 
    [3,] 0.20 0.250000 2.0000000 1.250000 10.0000000 8.00 
    [4,] 0.50 1.000000 0.1000000 2.000000 0.2000000 0.10 
    [5,] 0.60 0.600000 0.7500000 1.000000 1.2500000 1.25 

如果數據是在一個數據幀,而不是一個矩陣,則可以使用陣列的操作:

例如。讓我們假設上面的矩陣是A=as.data.frame(A)然後

combn(A,2,function(x)x[,1,]/x[,2,]) 
     [,1]  [,2]  [,3]  [,4]  [,5] [,6] 
[1,] 1.25 5.000000 10.0000000 4.000000 8.0000000 2.00 
[2,] 1.00 1.333333 0.6666667 1.333333 0.6666667 0.50 
[3,] 0.20 0.250000 2.0000000 1.250000 10.0000000 8.00 
[4,] 0.50 1.000000 0.1000000 2.000000 0.2000000 0.10 
[5,] 0.60 0.600000 0.7500000 1.000000 1.2500000 1.25 

,您仍然可以修改代碼,你的方式want.This只是一個大概的瞭解。希望它可以幫助

+0

combn(** 4,2 **,function(x)A [,x [1]]/A [,x [2]]) - 感謝您的及時回覆。爲了確認,4和2在這裏代表什麼。我的矩陣是716 * 274,所以我將不得不調整這些數字,我相信 – Rami

+0

4是你有變量的數量..和2是可供選擇的元素的數量4.你選擇2個元素..我也可以已經說過combn(ncol(A),2,....)' – Onyambu

+0

'combn'代表組合.. ..你有'x1,x2,x3,x4'。您只能選擇兩個:'x1&x2,x1&x3..etc'您將四個變量中的兩個相結合。使用'combn'這樣的函數來玩'combn(4,2)',看看你得到了什麼。你也可以'combn(4,2,simplified = FALSE)'如果簡化的不清楚 – Onyambu

1
dat <- structure(list(X1 = c(10L, 4L, 2L, 1L, 3L), X2 = c(8L, 4L, 10L, 
2L, 5L), X3 = c(2L, 3L, 8L, 1L, 5L), X4 = c(1L, 6L, 1L, 10L, 
4L)), .Names = c("X1", "X2", "X3", "X4"), class = "data.frame", row.names = c(NA, 
-5L)) 

當您使用應用逐行的基礎上,需要調換,結果按行獲取值:

t(# this is the last function to execute, we will need to convert to row basis 
    apply(dat, 1, # loop over rows, single row at a time 
    function(r){ apply(combn(r,2), 2, # now loop over columns of `combn` result 
            function(x) x[[1]]/x[[2]]) })) 
    [,1]  [,2]  [,3]  [,4]  [,5] [,6] 
[1,] 1.25 5.000000 10.0000000 4.000000 8.0000000 2.00 
[2,] 1.00 1.333333 0.6666667 1.333333 0.6666667 0.50 
[3,] 0.20 0.250000 2.0000000 1.250000 10.0000000 8.00 
[4,] 0.50 1.000000 0.1000000 2.000000 0.2000000 0.10 
[5,] 0.60 0.600000 0.7500000 1.000000 1.2500000 1.25