- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to apply one sample t-test on all columns of an R data frame?
- How to apply t test on each row of an R data frame?
- How to create a categorical variable using a data frame column in R?
- How to perform paired t test in R with a factor column in the data frame?
- How to check if a column is categorical in R data frame?
- How to test for significant relationship between two categorical columns of an R data frame?
- How to create a frequency column for categorical variable in an R data frame?
- How to find the mean of a numerical column by two categorical columns in an R data frame?
- Set values in categorical column to numeric values in R data frame.
- How to find the sample size for t test in R?
- How to subset an R data frame based on numerical and categorical column?
- Create a sample of data frame column by excluding NAs in R
- How to find the column variance if some columns are categorical in R data frame?
- How to find the column means if some columns are categorical in R data frame?
- How to find the column median if some columns are categorical in R data frame?

Advertisements