Help with multiple sample t-tests

Hello,

I need some help in figuring out how to perform mutli-sample t-tests in R. I'm a novice in R but have basic knowledge. I'm having issues with doing t-tests and calculating p-values (including adjusted p-values). The dataset i'm working with looks like the image below. I'd like to be able to perform individual t-tests comparing Study day 28 with study day 7 and study day 0, for each population (i.e. for A, B, C, D, E). My actual dataset has hundreds of columns and more rows but the basic concept is the same. Any help is appreciated. Thanks

Capture

First of all, please read FAQ on how to provide a reprex (reproducible example): FAQ: What's a reproducible example (`reprex`) and how do I create one?

As for your question, I guess you need something like the following.

library(tidyverse)
library(rstatix)

set.seed(123)
df <- tibble(ID = rep(1:3, times = 3),
       Study_day = rep(1:3, each = 3),
       A = rnorm(9, mean = 50, sd = 10),
       B = c(rnorm(3, mean = 20, sd = 5),
             rnorm(3, mean = 50, sd = 5),
             rnorm(3, mean = 90, sd = 10)))

# As your data is not in tidy format, you first need to tidy it
df <- df %>%
  pivot_longer(cols = A:B,
               names_to = "Population",
               values_to = "Value")

# Now you can run t-test on your data
t_test_results <- df %>%
  # As pairwise_t_test() does not work with functions there 2+ variables
  # on the right side, you have to combine them in a single variable
  mutate(groups = interaction(Population, Study_day)) %>%
  # As you don't need comparison between populations use gruoping
  group_by(Population) %>%
  # run t-test. Please check ?pairwise_t_test for additional arguments
  # perhaps you'll want to add `paired = TRUE`
  pairwise_t_test(formula = Value ~ groups)

# As you want to compare day 28 vs day 7 and day 28 vs day 0
# you may want to filter out all day 7 vs day 0
t_test_results %>%
  filter(str_detect(group2, pattern = "3"))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.