How to run multiple t.test in a data.frame?

前端 未结 1 1760
名媛妹妹
名媛妹妹 2021-01-29 05:23

I have a data,

temp_data <- as.data.frame(matrix(rbinom(9*9, 1, 0.5), ncol=9, nrow =9))
colnames(temp_data) <- paste(rep(c(\"a\",\"b\",\"c\"), each=3), re         


        
相关标签:
1条回答
  • 2021-01-29 05:54

    You could do this:

    pa <- unlist(lapply(1:3, function(i) t.test(temp_data[, i], temp_data[, i + 6])$p.value))
    pb <- unlist(lapply(1:3, function(i) t.test(temp_data[, i + 3], temp_data[, i + 6])$p.value))
    p <- c(pa, pb)
    p.table <- as.matrix(data.frame(factor.1 = colnames(temp_data[1:6]),
               factor.2 = rep(colnames(temp_data[7:9]), 2),
               p.value = p))
    p.table
    #      factor.1 factor.2 p.value      
    # [1,] "a1"     "c1"     "0.652543514"
    # [2,] "a2"     "c2"     "0.176463197"
    # [3,] "a3"     "c3"     "0.176463197"
    # [4,] "b1"     "c1"     "0.372458894"
    # [5,] "b2"     "c2"     "0.003949773"
    # [6,] "b3"     "c3"     "0.652543514"
    
    0 讨论(0)
提交回复
热议问题