2017-08-16 71 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