create a 2 paired t test on two random datasets of numbers in r

To perform a 2 paired t-test in R on two random datasets of numbers, you can follow these steps:

  1. Create two vectors or data frames of random data:
main.r
set.seed(123) # for reproducibility
data1 <- rnorm(20, mean = 10, sd = 2)
data2 <- rnorm(20, mean = 8, sd = 2)
111 chars
4 lines
  1. Calculate the difference between the pairs:
main.r
dif <- data1 - data2
21 chars
2 lines
  1. Perform the paired t-test using the t.test() function:
main.r
t.test(data1, data2, paired = TRUE)
36 chars
2 lines

The output will include the t-statistic, degrees of freedom, p-value, 95% confidence interval, and the mean difference.

It should be noted that the paired t-test assumes that the differences between the pairs are normally distributed. If this assumption is violated, another type of test may be necessary.

gistlibby LogSnag