我有一个数据,
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), rep(c(1,2,3), 3), sep = "")
我想对data.frame的因子运行多个t.test并获取显示此内容的输出文件
factor.1 factor.2 p.value
[1,] "a1" "c1" "value 1 "
[2,] "a2" "c2" "value 2 "
[3,] "a3" "c3" "value 3 "
[4,] "b1" "c1" "value 4 "
[5,] "b2" "c2" "value 5 "
[6,] "b3" "c3" "value 6 "
如何获得此结果?
答案 0 :(得分:2)
你可以这样做:
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"