- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
Consider the below data frame −
x1<-sample(c("Male","Female"),20,replace=TRUE) y1<-rpois(20,5) df1<-data.frame(x1,y1) df1
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 −
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
x2<-sample(c("Hot","Cold"),20,replace=TRUE) y2<-sample(0:9,20,replace=TRUE) df2<-data.frame(x2,y2) df2
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 −
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
- How to perform paired t test for multiple columns in R?
- How to apply two sample t test using a categorical column in R data frame?
- How to extract the factor levels from factor column in an R data frame?
- How to sort a numerical factor column in an R data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to perform Wilcoxon test for all columns in an R data frame?
- How to perform shapiro test for all columns in an R data frame?
- How to find the most frequent factor value in an R data frame column?
- How to match a column in a data frame with a column in another data frame in R?
- How to apply t test on each row of an R data frame?
- Extract a particular level from factor column in an R data frame.
- How to set a level of a factor column in an R data frame to NA?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to collapse factor levels in an R data frame?
- How to subset factor columns in an R data frame?