Search code examples
rt-test

I want to run a t-test, how should I organise my data to do so?


In R, I want to run a statistical test to compare the averages between two categories, but I do not know how to organise my data to do so.

Mock example

My data is organised like:

structure(list(age = c(39, 45, 83, 68, 48, 52, 66, 50, 61, 67), gender = 
structure(c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("female", 
"male"), class = "factor")), .Names = c("age", "gender"), row.names = c(NA, 
10L), class = "data.frame")

What I want to do is compare the average of each gender with a Welch t-test, answering the question "do women's ages are significantly different from men's ages?".

Theoretically, to run the test, I think my data should be in the form:

male  female
39    45
83    61
...

I'm sure there is either a way to run the test directly on the original table or an easy way to transform my data into this form...

So, how should I proceed?


Solution

  • If df is your data set, you can do

    t.test(age ~ gender, data=df, alternative='two.sided')
    

    and there's no need to reorganise the data.