R:如何使用多个参数打印表

时间:2013-06-24 11:05:55

标签: r vector sapply

我使用以下方法从更精确的测试中获取p值。但是,我需要为每对打印值“n00,n01,n10,n11”。如何将其打印为表格而不是下面的矩阵以及p值?

fish <- function(y, x) {
    n00 = sum((1-x)*(1-y))
                       n01 = sum((1-x)*y)
                       n10 = sum(x*(1-y))
                       n11 = sum(x*y)
                       a = matrix(c(n00, n01, n10, n11)
                                  , nrow = 2)
                       pval = fisher.test(a)$p.value
                       return(pval)
}

chiArray <- function(x) apply(mat1, 1, fish, x)
sapply(1:nrow(mat2), function(j) chiArray(mat2[j, ]))
chisq.cna.mut.test <- sapply(1:nrow(mat2), function(j) chiArray(mat2[j, ]))

我希望输出为:

# name1_mat1 name1_mat2 n00 n01 n10 n11 pvalue
# name1_mat1 name2_mat2 n00 n01 n10 n11 pvalue

输入是(mat1),为了这个例子,考虑mat2与mat1相同。

# ID    case1   case2   case3
# name1 0   0   0
# name2 1   0   1
# name3 0   1   1

1 个答案:

答案 0 :(得分:0)

使用由此生成的数据:

ID <- paste0("name", 1:5)
mat1 <- 1*cbind(case1=runif(5)>0.5, case2=runif(5)>0.5, case3=runif(5)>0.5)
mat2 <- 1*cbind(case1=runif(5)>0.5, case2=runif(5)>0.5, case3=runif(5)>0.5)
rownames(mat1) <- ID
rownames(mat2) <- ID

更改您的函数以返回所有n以及p值:

fish <- function(y, x)
{
    n00 = sum((1-x)*(1-y))
    n01 = sum((1-x)*y)
    n10 = sum(x*(1-y))
    n11 = sum(x*y)
    a = matrix(c(n00, n01, n10, n11), nrow = 2)
    pval = fisher.test(a)$p.value
    c(a, pval)
}

我认为你想要的就是这个:

result <- data.frame(mat1="", mat2="", n00=0, n01=0, n10=0, n11=0, pvalue=0, stringsAsFactors=FALSE)

k <- 1

for(j in 1:nrow(mat2)) for(i in 1:nrow(mat1))
{
    result[k,1] <- paste0(rownames(mat1)[i], "_mat1")
    result[k,2] <- paste0(rownames(mat2)[j], "_mat2")
    result[k,-(1:2)] <- fish(x=mat2[j, ], y=mat1[i,])
    k <- k + 1
}

示例:

> result
         mat1       mat2 n00 n01 n10 n11    pvalue
1  name1_mat1 name1_mat2   1   1   0   1 1.0000000
2  name2_mat1 name1_mat2   0   2   0   1 1.0000000
3  name3_mat1 name1_mat2   2   0   1   0 1.0000000
4  name4_mat1 name1_mat2   1   1   1   0 1.0000000
5  name5_mat1 name1_mat2   0   2   1   0 0.3333333
6  name1_mat1 name2_mat2   0   1   1   1 1.0000000
7  name2_mat1 name2_mat2   0   1   0   2 1.0000000
8  name3_mat1 name2_mat2   1   0   2   0 1.0000000
9  name4_mat1 name2_mat2   1   0   1   1 1.0000000
10 name5_mat1 name2_mat2   1   0   0   2 0.3333333
11 name1_mat1 name3_mat2   0   1   1   1 1.0000000
12 name2_mat1 name3_mat2   0   1   0   2 1.0000000
13 name3_mat1 name3_mat2   1   0   2   0 1.0000000
14 name4_mat1 name3_mat2   1   0   1   1 1.0000000
15 name5_mat1 name3_mat2   0   1   1   1 1.0000000
16 name1_mat1 name4_mat2   0   1   1   1 1.0000000
17 name2_mat1 name4_mat2   0   1   0   2 1.0000000
18 name3_mat1 name4_mat2   1   0   2   0 1.0000000
19 name4_mat1 name4_mat2   1   0   1   1 1.0000000
20 name5_mat1 name4_mat2   1   0   0   2 0.3333333
21 name1_mat1 name5_mat2   0   1   1   1 1.0000000
22 name2_mat1 name5_mat2   0   1   0   2 1.0000000
23 name3_mat1 name5_mat2   1   0   2   0 1.0000000
24 name4_mat1 name5_mat2   1   0   1   1 1.0000000
25 name5_mat1 name5_mat2   1   0   0   2 0.3333333