[R] Form Pairs of Variables for a paired t-test

Muenchen, Robert A (Bob) muenchen at utk.edu
Wed Jan 30 21:22:32 CET 2008


That's a dandy little program but the apply with lapply blew my mind! I had to pick it apart to figure out what it was doing. Perhaps others will find this expanded version useful:

# Make up some repeated measures data with measures at 4 times.
t1<-c(1,2,3,4,5)
t2<-c(2,3,3,5,5)
t3<-c(3,3,4,4,4)
t4<-c(5,6,6,7,7)
myTimes<-data.frame(t1,t2,t3,t4)
myTimes

# Get matrix of combinations of 4 things taken two at a time.
myCombos<-combn( ncol(myQs), 2 )
myCombos

# Generate a list that contains all the pairs of times.
myTimesList <- apply( myCombos, 2, function(y) myTimes[ ,y] )
myTimesList

# apply the t.test function to each set of pairs in the myTimeCombos
lapply( myTimesList, 
        function(z) t.test( z[ ,1], z[ ,2] ) 
)

Cheers,
Bob

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Henrique Dallazuanna
> Sent: Tuesday, January 29, 2008 9:15 AM
> To: nalluri pratap
> Cc: r-help at r-project.org
> Subject: Re: [R] Form Pairs of Variables for a paired t-test
> 
> Try this:
> 
> lapply(apply(combn(ncol(x),2), 2, function(y)x[,y]),
> function(z)t.test(z[,1], z[,2]))
> 
> On 29/01/2008, nalluri pratap <pratap_stat at yahoo.co.in> wrote:
> > Hi Users,
> >
> >   This is regarding the paired t-test. I have 5 variables (say)
> Data$v1,Data$v2,Data$v3,Data$v4,Data$v5 in my data frame. Now, I need
> to perform a paired t-test on all the possible 10 pairs.How do I set up
> the pairs table directly and pass those variables in to t-test.
> >
> >   Thanks in advance,
> >
> >   Pratap
> >
> >
> > ---------------------------------
> >  Now you can chat without downloading messenger. Click here to know
> how.
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list