-1

Is there a way to extract the mean and p-value from a t.test output and create a table that includes the features, mean, and p-value? Say there are 10 columns put through t.test, and that means there are 10 means, and 10 p-values. How would I be able to create a table which only shows those specific items?

here is an example: data (iris):

    a.   b.  c.  d.   e. 
1   5.1 3.5 1.4 0.2 setosa
2   4.9 3.0 1.4 0.2 setosa
3   4.7 3.2 1.3 0.2 setosa
4   4.6 3.1 1.5 0.2 setosa
5   5.0 3.6 1.4 0.2 setosa
6   5.4 3.9 1.7 0.4 setosa

t.test(a)
t.test(b) #...ect we obtain the mean and p-value. 

this is the output im looking for:

feature mean p-val
col1  0.01 0.95
col2  0.01 0.95
.
.
.
coln

hope it makes sense!

  • Do not post images of code or data. We cannot copy data from images into R for testing. See [how to include data in a reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Nov 27 '21 at 08:15
  • I don't get the aim.. you are testing the mean of each column is not zero? – StupidWolf Nov 27 '21 at 09:24
  • I have updated my answer to give you the output as desired - please accept the answer if it solves your problem – rg255 Nov 27 '21 at 09:32

1 Answers1

1

Using the iris built in data set as an example

t(sapply(iris[, 1:4], function(i){
  t.test(i)[c(5,3)]
}))

The sapply() function is iteratively performing that custom function - which performs a t-test on a variable and returns the estimate and p-value - through columns 1 to 4 of iris. That is then transposed by t() to rotate the data to the desired shape. You can store that as a data.frame using data.frame() and use row.names() to get the variable names into a new column on that if you like.

values <- t(sapply(iris[, 1:4], function(i){
  t.test(i)[c(5,3)]
}))

values <- data.frame("feature"=row.names(values), values)
row.names(values) <- NULL
values

Beware multiple testing though...

rg255
  • 4,119
  • 3
  • 22
  • 40