say.ff
say.ff

Reputation: 395

Apply t.test on a tidy format data

I have a data frame in tidy format as follows:

df <- data.frame(name = c("A", "C", "B", "A", "B", "C", "D") ,
             group = c(rep("case", 3), rep("cntrl", 4)), 
              mean = rnorm(7, 0,1))

I would like to group the data by two variables name and group and apply a t.test on mean value of each category. For example doing t.test between A_case.vs.A_cntrl and add pvalue as the result to the table.

Do you have any idea how can I do this using tidyverse package?

Thanks,

Upvotes: 0

Views: 587

Answers (1)

akrun
akrun

Reputation: 887118

here, a group wise, t.test on 'name' cannot be performed as there is only a single observation for each pair. Instead, we can do

library(dplyr)
df %>% 
   summarise(ttest = list(t.test(mean[group == 'case'],
                                  mean[group == 'cntrl']))) %>% 
   pull(ttest)

Update

If we need to create a column, use mutate

df %>% 
    mutate(pval = t.test(mean[group == 'case'],
                               mean[group == 'cntrl'])$p.value)

Or reshape to 'wide' format and then do the t.test on the columns

library(tidyr)
df %>% 
   pivot_wider(names_from = group, values_from = mean) %>% 
   summarise(ttest = list(t.test(case, cntrl))) %>% 
   pull(ttest)

Upvotes: 1

Related Questions