- 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 perform paired t test in R with a factor column in the data frame?
When we have a factor column in an R data frame that has two levels and a numerical column then we can apply paired-test on this data frame but the data must be collected for same subjects, otherwise it will not be a paired data. The t.test application on the data discussed here can be done by using the command t.test(y1~x1,data=df), where y1 is the numerical column, x1 is the factor column, and both these columns are stored in data frame called df.
Example
Consider the below data frame −
x1<-sample(c("Male","Female"),20,replace=TRUE) y1<-rpois(20,5) df1<-data.frame(x1,y1) df1
Output
x1 y1 1 Female 4 2 Male 4 3 Female 4 4 Male 4 5 Female 6 6 Male 4 7 Female 3 8 Male 4 9 Female 7 10 Male 6 11 Male 2 12 Female 1 13 Male 5 14 Male 8 15 Male 6 16 Male 6 17 Female 3 18 Female 5 19 Male 4 20 Male 5
Applying the t.test on data in df1 −
Example
t.test(y1~x1,data=df1)
Output
Welch Two Sample t-test data: y1 by x1 t = -0.88636, df = 12.897, p-value = 0.3917 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -2.436194 1.019527 sample estimates: mean in group Female mean in group Male 4.125000 4.833333
Example
x2<-sample(c("Hot","Cold"),20,replace=TRUE) y2<-sample(0:9,20,replace=TRUE) df2<-data.frame(x2,y2) df2
Output
x2 y2 1 Hot 8 2 Cold 1 3 Hot 5 4 Hot 2 5 Cold 4 6 Cold 0 7 Hot 8 8 Cold 3 9 Cold 9 10 Cold 6 11 Cold 0 12 Cold 9 13 Hot 6 14 Hot 2 15 Cold 3 16 Hot 1 17 Cold 6 18 Hot 7 19 Hot 8 20 Hot 9
Applying the t.test on data in df2 −
Example
t.test(y2~x2,data=df2)
Output
Welch Two Sample t-test data: y2 by x2 t = -1.0627, df = 17.721, p-value = 0.3022 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -4.46872 1.46872 sample estimates: mean in group Cold mean in group Hot 4.1 5.6
Advertisements
To Continue Learning Please Login
Login with Google