Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...