![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 one sample t-test on all columns of an R data frame?
When we want to apply t-test on columns of a data frame then we generally perform them one by one by accessing the appropriate columns but if we want to apply the test on all columns of the data frame then we can take the help of sapply function. For example, if we have a data frame called df that contains multiple columns then the one sample-test can be applied to all columns using the command sapply(df,t.test).
Example1
Consider the below data frame −
> x1<-rnorm(20) > x2<-rnorm(20,5,2.5) > x3<-rnorm(20,5,0.31) > df1<-data.frame(x1,x2,x3) > df1
Output
x1 x2 x3 1 -2.25470472 2.730284 5.257561 2 0.31811059 4.978190 4.799871 3 0.53974937 5.247124 4.533089 4 0.04855004 4.285427 5.187038 5 0.23913269 6.424229 5.335241 6 1.41318324 5.184035 5.638625 7 -1.19378598 0.729062 5.065400 8 0.53453582 10.548991 4.349061 9 -0.90284652 2.019270 5.479600 10 -1.85263184 5.272444 5.148048 11 0.20052589 7.367680 4.806746 12 -0.67952841 7.784398 4.908527 13 1.34338527 4.627357 5.090057 14 0.50015711 3.983630 4.370341 15 1.77264005 3.099567 4.930903 16 -0.55921578 -1.081910 5.597464 17 -0.46257623 4.273301 5.045864 18 0.97870030 6.683427 5.051728 19 -0.96029384 10.277885 4.978401 20 -1.33514505 5.534050 4.558999
Applying t-test on all columns of df1 −
> sapply(df1,t.test)
Output
x1 x2 x3 statistic -0.4687486 7.892499 60.87446 parameter 19 19 19 p.value 0.6445828 2.047736e-07 3.024457e-23 conf.int Numeric,2 Numeric,2 Numeric,2 estimate -0.1156029 4.998422 5.006628 null.value 0 0 0 stderr 0.2466203 0.633313 0.08224513 alternative "two.sided" "two.sided" "two.sided" method "One Sample t-test" "One Sample t-test" "One Sample t-test" data.name "X[[i]]" "X[[i]]" "X[[i]]"
Example2
> y1<-rpois(20,5) > y2<-rpois(20,2) > y3<-rpois(20,5) > df2<-data.frame(y1,y2,y3) > df2
Output
y1 y2 y3 1 4 2 4 2 4 1 2 3 6 1 5 4 4 3 7 5 5 4 2 6 4 4 5 7 7 3 6 8 4 0 8 9 3 0 4 10 4 4 4 11 5 2 3 12 8 2 7 13 4 4 3 14 8 1 6 15 7 3 7 16 3 2 6 17 4 0 1 18 1 3 3 19 2 3 2 20 3 4 4
Applying t-test on all columns of df2 −
> sapply(df2,t.test)
Output
y1 y2 y3 statistic 10.71684 7.254174 9.888889 parameter 19 19 19 p.value 1.706058e-09 6.946248e-07 6.298981e-09 conf.int Numeric,2 Numeric,2 Numeric,2 estimate 4.5 2.3 4.45 null.value 0 0 0 stderr 0.4198997 0.3170589 0.45 alternative "two.sided" "two.sided" "two.sided" method "One Sample t-test" "One Sample t-test" "One Sample t-test" data.name "X[[i]]" "X[[i]]" "X[[i]]"
Advertisements