Search code examples
rstatisticst-test

R t.test group by


I have stumbled across a bit of an annoying problem. I am trying to perform multiple independent sample t-tests at once, grouped by a value.

To put it into an example: In 5 cities we have measured job satisfaction between males and females. For every subject we know their city, their gender, and their satisfaction score. We can do a t-test over the whole sample like this:

t.test(Score ~ Gender, paired = FALSE)

However, I want to perform a t-test for each city, to see if there is a difference in the average job satisfaction per gender per city. How do I do this?


Solution

  • You can use lapply with split to do a group by t.test,

    lapply(split(mtcars, factor(mtcars$cyl)), function(x)t.test(data=x, mpg ~ am, paired=FALSE))
    

    Here I have used mtcars data, and performed a independent t.test by using cyl as group to perform a t.test on mpg(continuous data) and am (categorical data). Let me know if this is not you are expecting.