Independent and Paired T-test using R Worksheet

  • In this worksheet you will learn how to use R to perform an independent or Paired T-test. This test takes 2 groups which are unpaired or paired (see suggested video below) and calculates if there is a statistically significant difference between them.

Important notes

  • You cannot do repeated T-tests if you have multiple groups. T-test is only for comparing two groups. If you have 3 or more groups you should do One Way ANOVA.

Required prerequisite(s)

Suggested prerequisite(s)

Dataset

Unpaired Steps

  1. Open RStudio
  2. Read in the UnpairedDataset1.tsv file. The read.delim will automatically assign row 1 as a header so no extra flags need to be passed to it
    unpaired <- read.delim(UnpairedDataset1.tsv")
    
  3. T-tests are only performed on two groups, but our dataset has 3 groups. We will select just Group 1 and Group 2 for the test
    unpairedGroups <- unpaired[,c("Group.1","Group.2")]
    
  4. To perform a t-test the data must in a ‘melted’ long format. Install the reshape 2 package (if needed) and then load the library
    install.packages("reshape2")
    library("reshape2")
    
  5. Melt the data frame so it is in the right format for the T-test
    meltedUnpairedGroups=melt(unpairedGroups)
    
  6. Perform the t-test and store in a variable
    up_ttest = t.test(value ~ variable, data=meltedUnpairedGroups)
    
  7. View the p-value of the test
    up_ttest$p.value
    

    Paired Steps

  8. Open RStudio
  9. Read in the Paired.Dataset1.tsv file. This file has sample names in column 1 so you must pass that flag to read.delim
    paired <- read.delim("PairedDataset1.tsv", row.names=1)
    
  10. T-tests are only performed on two groups, but our dataset has 3 groups. We will select just Condition 1 and Condition 2 for the test
    pairedGroups <- paired[,c("Condition.1","Condition.2")]
    
  11. To perform a t-test the data must in a ‘melted’ long format. Install the reshape 2 package (if needed) and then load the library
    install.packages("reshape2")
    library("reshape2")
    
  12. Melt the data frame so it is in the right format for the T-test
    meltedPairedGroups=melt(pairedGroups)
    
  13. Perform the t-test and store in a variable. Note the use of the paired flag for paired data
    p_ttest = t.test(value ~ variable, data=meltedPairedGroups , paired= TRUE)
    
  14. View the p-value of the test
    p_ttest$p.value