Deep
Deep

Reputation: 77

One sample ttest pvalue in dplyr summarise_each

I would like to calculate a few summary statistics and the one sample ttest p value for various groups in a dataframe.

I was able to obtain the mean, max, min, count etc., but could not get the pvalue

Here is the code that worked with out the one sample ttest

library(dplyr)
library(ggplot2)
library(nlmeU)

data(armd0, package = "nlmeU")
df <- armd0 %>%
  group_by(treat.f, time.f) %>%
  summarise_each(funs(mean, median, min, max, length, sd), visual)

I would like to obtain the output of t.test$p.value in the same table.

Thank you for your help!

Upvotes: 1

Views: 506

Answers (1)

aosmith
aosmith

Reputation: 36104

For a one sample t-test that the mean of the group is not 0, you can use t.test directly in funs, pulling out the p-value with dollar sign notation. You'll need to give the new column a nicer name, though.

summarise_each(funs(mean, median, min, max, length, sd, pvalue = t.test(.)$p.value), visual)

Upvotes: 1

Related Questions