I can run one-sample t-test on an array, for example a matrix myData1, with the following apply(myData1, 2, t.test) Is there a similar fashion using apply() or something else to run 2-sample t-test with datasets from two groups, myData1 and myData2, without looping? TIA, Gang
I think that you can use mapply for this. On Tue, Jan 6, 2009 at 3:24 PM, Gang Chen <gangchen6@gmail.com> wrote:> I can run one-sample t-test on an array, for example a matrix myData1, > with the following > > apply(myData1, 2, t.test) > > Is there a similar fashion using apply() or something else to run > 2-sample t-test with datasets from two groups, myData1 and myData2, > without looping? > > TIA, > Gang > > ______________________________________________ > R-help@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 [[alternative HTML version deleted]]
Thanks a lot for the quick help! mapply() seems promising. However, mapply(t.test, myData1, myData2) would not work, so how can I specify the margin in mapply() which function t.test() will be applied over? For example, I specify the 2nd dimension (column) in apply(myData1, 2, t.test) to run one-sample t-test. Is there a way I can achieve the same with mapply()? Thanks again, Gang On Tue, Jan 6, 2009 at 12:34 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:> I think that you can use mapply for this. > > On Tue, Jan 6, 2009 at 3:24 PM, Gang Chen <gangchen6 at gmail.com> wrote: >> >> I can run one-sample t-test on an array, for example a matrix myData1, >> with the following >> >> apply(myData1, 2, t.test) >> >> Is there a similar fashion using apply() or something else to run >> 2-sample t-test with datasets from two groups, myData1 and myData2, >> without looping? >> >> TIA, >> Gang
Perhaps you can convert your matrices to data frames as in: mapply(t.test,as.data.frame(myData1),as.data.frame(myData2)) to test by column and mapply(t.test,as.data.frame(t(myData1)),as.data.frame(t(myData2))) to test by row? ----- Original Message ---- From: Gang Chen <gangchen6 at gmail.com> To: Henrique Dallazuanna <wwwhsd at gmail.com> Cc: r-help at stat.math.ethz.ch Sent: Tuesday, January 6, 2009 10:10:44 AM Subject: Re: [R] Using apply for two datasets Thanks a lot for the quick help! mapply() seems promising. However, mapply(t.test, myData1, myData2) would not work, so how can I specify the margin in mapply() which function t.test() will be applied over? For example, I specify the 2nd dimension (column) in apply(myData1, 2, t.test) to run one-sample t-test. Is there a way I can achieve the same with mapply()? Thanks again, Gang On Tue, Jan 6, 2009 at 12:34 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:> I think that you can use mapply for this. > > On Tue, Jan 6, 2009 at 3:24 PM, Gang Chen <gangchen6 at gmail.com> wrote: >> >> I can run one-sample t-test on an array, for example a matrix myData1, >> with the following >> >> apply(myData1, 2, t.test) >> >> Is there a similar fashion using apply() or something else to run >> 2-sample t-test with datasets from two groups, myData1 and myData2, >> without looping? >> >> TIA, >> Gang______________________________________________ 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.
Thanks a lot for the suggestions, Jorge and Satoshi Takahama! Both approaches work well... Gang On Tue, Jan 6, 2009 at 2:12 PM, Satoshi Takahama <s.takahama at yahoo.com> wrote:> Perhaps you can convert your matrices to data frames as in: > > mapply(t.test,as.data.frame(myData1),as.data.frame(myData2)) > to test by column and > > mapply(t.test,as.data.frame(t(myData1)),as.data.frame(t(myData2))) > > > to test by row? > > > ----- Original Message ---- > From: Gang Chen <gangchen6 at gmail.com> > To: Henrique Dallazuanna <wwwhsd at gmail.com> > Cc: r-help at stat.math.ethz.ch > Sent: Tuesday, January 6, 2009 10:10:44 AM > Subject: Re: [R] Using apply for two datasets > > Thanks a lot for the quick help! > > mapply() seems promising. However, mapply(t.test, myData1, myData2) > would not work, so how can I specify the margin in mapply() which > function t.test() will be applied over? For example, I specify the 2nd > dimension (column) in apply(myData1, 2, t.test) to run one-sample > t-test. Is there a way I can achieve the same with mapply()? > > Thanks again, > Gang > > > On Tue, Jan 6, 2009 at 12:34 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote: >> I think that you can use mapply for this. >> >> On Tue, Jan 6, 2009 at 3:24 PM, Gang Chen <gangchen6 at gmail.com> wrote: >>> >>> I can run one-sample t-test on an array, for example a matrix myData1, >>> with the following >>> >>> apply(myData1, 2, t.test) >>> >>> Is there a similar fashion using apply() or something else to run >>> 2-sample t-test with datasets from two groups, myData1 and myData2, >>> without looping? >>> >>> TIA, >>> Gang
It depends on how the data are arranged ##---------------------------------------------------- x<-matrix(c(1,2,3,2,8,2,4,5,6),nrow=3) y<-matrix(c(10,2,13,0,8,4,4.2,5.2,6.2),nrow=3) q<-mapply(t.test,as.data.frame(x),as.data.frame(y)) q ## The ith column of q contain the results of applying t.test to ## the ith column of x and the jth column of y ##---------------------------------------------------- Since the t.test returns a list, you can wrap it in your own function if you want to process the data in an assembly line fashion. Continuing the previous example: my.t<-function(x,y,...) { c(t.test(x,y,...))[1:3] } q2<-mapply(my.t,as.data.frame(x),as.data.frame(y)) q2 Good luck! Christos Argyropoulos University of Pittsburgh Medical Center