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!
Using the iris built in data set as an example
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.
Beware multiple testing though...
We could do something like this. Here with an example dataset of 3 columns aa grouping column.
#example dataset
df <- mtcars %>%
mutate(vs = factor(vs)) %>%
select(mpg, disp, hp, vs)
means = colMeans(df[sapply(df, is.numeric)])
p.value= sapply(df[,1:3], function(i) t.test(i ~ df$vs)$p.value)
rbind(means, p.value)
mpg disp hp
means 2.009062e+01 2.307219e+02 1.466875e+02
p.value 1.098368e-04 2.476526e-06 1.819806e-06
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.