使用数据框中的应用功能对基因进行T检验

时间:2018-08-19 00:56:12

标签: r dataframe p-value

我正在尝试对两个数据帧进行t.test。

数据帧(我是从data.frame雕刻出来的)具有我需要到行1:143的数据。我已经按照计算rowMeans的需要创建了子变量。

> c.mRNA<-rowMeans(c007[1:143,(4:9)])
> h.mRNA<-rowMeans(c007[1:143,(10:15)])

我只是想对每行进行一次t.test,然后将p值绘制为直方图。我认为这是行得通的……

Pvals<-apply(mRNA143.data,1,function(x) {t.test(x[c.mRNA],x[h.mRNA])$p.value})

但是我总是收到错误消息?

Error in t.test.default(x[c.mRNA], x[h.mRNA]) : 
  not enough 'x' observations

我的语法有些不足,无法终生解决!

编辑:我已经创建了一个data.frame,所以它现在只有两列,我需要为每一行提供一个p值。以下是我的数据示例...

      c.mRNA    h.mRNA
1    8.224342  8.520142
2    9.096665 11.762597
3   10.698863 10.815275
4   10.666233 10.972130
5   12.043525 12.140297

我尝试过了...

 pvals=apply(mRNA143.data,1,function(x) {t.test(mRNA143.data[,1],mRNA143.data[, 2])$p.value})

但是我可以从我的情节中得知我不在(情节是一条直线)。

1 个答案:

答案 0 :(得分:0)

一个可复制的例子将大有帮助。在准备它时,您可能已经意识到您正在尝试根据均值对列进行子集设置,这实际上是没有意义的。

您要做的是遍历数据行,属于某个组的子集列,对第二组重复此操作,并将其传递给t.test函数。

这就是我要做的。

group1 <- matrix(rnorm(50, mean = 0, sd = 2), ncol = 5)
group2 <- matrix(rnorm(50, mean = 5, sd = 2), ncol = 5)

xy <- cbind(group1, group2)

# this is just a visualization of the test you're performing
plot(0, 0, xlim = c(-5, 11), ylim = c(0, 0.25), type = "n")
curve(dnorm(x, mean = 5, sd = 2), add = TRUE)
curve(dnorm(x, mean = 0, sd = 2), add = TRUE)

out <- apply(xy, MARGIN = 1, FUN = function(x) {
  # x is a vector, e.g. xy[i, ] or xy[1, ]
  t.test(x = x[1:5], y = x[6:10])$p.value
})
out