How to apply two sample t test using a categorical column in R data frame?


To apply two sample t test using a categorical column in R data frame, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use t.test function with categorical column and numerical column linked with ~ sign.

Example

Create the data frame

Let’s create a data frame as shown below −

Gender<-sample(c("Male","Female"),30,replace=TRUE)
Score<-sample(1:10,30,replace=TRUE)
df<-data.frame(Gender,Score)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   Gender Score
1  Female 5
2  Female 6
3  Male   3
4  Male   7
5  Female 8
6  Female 6
7  Male  10
8  Male   9
9  Male  10
10 Male   6
11 Female 3
12 Male   2
13 Female 7
14 Male   6
15 Male   4
16 Female 5
17 Female 3
18 Female 8
19 Male   1
20 Male   5
21 Male   3
22 Male   1
23 Male   4
24 Female 1
25 Male   6
26 Male  10
27 Female 5
28 Male   2
29 Male   1
30 Male   9

Perform the t test using categorical column

Using t.test function with gender column and Score column linked with ~ sign as shown below −

Gender<-sample(c("Male","Female"),30,replace=TRUE)
Score<-sample(1:10,30,replace=TRUE)
df<-data.frame(Gender,Score)
t.test(Score~Gender,data=df)

Output

Welch Two Sample t-test

data: Score by Gender
t = 1.2604, df = 24.862, p-value = 0.2192
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.8460465 3.5127132
sample estimates:
mean in group Female mean in group Male
6.166667 4.833333

Updated on: 16-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements