T-Test For Genes using Apply Function in Dataframe

[亡魂溺海] 提交于 2019-12-13 08:19:45

问题


I’m trying to run a t.test on two data frames.

The dataframes (which I carved out from a data.frame) has the data I need to rows 1:143. I’ve already created sub-variables as I needed to calculate rowMeans.

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

I’m simply trying to run a t.test for each row, and then plot the p-values as histograms. This is what I thought would work…

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

But I keep getting an error?

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

I’ve got something off in my syntax and cannot figure it out for the life of me!

EDIT: I've created a data.frame so it's now just two columns, I need a p-value for each row. Below is a sample of my data...

      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

I tried this...

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

But I can tell from my plot that I'm off (the plots are in a straight line).


回答1:


A reproducible example would go a long way. In preparing it, you might have realized that you are trying to subset columns based on mean, which doesn't make sense, really.

What you want to do is go through rows of your data, subset columns belonging to a certain group, repeat for the second group and pass that to t.test function.

This is how I would do it.

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


来源:https://stackoverflow.com/questions/51913634/t-test-for-genes-using-apply-function-in-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!