# t-test “by hand”

```# R from zero to hero # Marco Plebani 18 May 2018 # let's perform a T test "by hand" (with a little help from R)```

Enter the data. You could enter them in Excel, save them as a csv file and then open them in R using `read.csv("FILE DIRECTORY HERE/twogroups.csv")`.
In this case it’s a handful of data so I’m doing it by hand as follows:

```# Group 1 g1 <- c(6,7,7.2,8,9) # Group 2 g2 <- c(1.5,2.5,2.6,5,5.5)```

`# t = (mean.g1 - mean.g2) / SE(mean.g1 - mean.g2)`

```mean.g1 <- mean(g1) mean.g2 <- mean(g2)```

`se.g1g2 <- sqrt((sd(g1)^2)/length(g1) + (sd(g2)^2)/length(g2))`

`t.value <- (mean.g1 - mean.g2)/se.g1g2`

For the t test, df = number of values in g1 + number of values in g2 – 2
In R:

`degs.of.freedom <- length(g1) + length(g2) - 2`
```# we can either look it up un a table, or ask R! qt(p=0.95, df = degs.of.freedom)```
`# qf() is a function that gives you the Quantiles for the t distribution - basically it's a t-value calculator. Like a table of t values, just fancier.`

So, are the two groups significantly different?