[R] Looped t.test results according to a subset variable

jim holtman jholtman at gmail.com
Tue Oct 23 01:41:50 CEST 2007


Is this what you were expecting?

> by(x, x$quiz, function(.sub) t.test(percent ~ group, data=.sub))
x$quiz: 1

        Welch Two Sample t-test

data:  percent by group
t = 6.3228, df = 6.231, p-value = 0.0006306
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 30.27235 67.94193
sample estimates:
mean in group High  mean in group Low
          92.85714           43.75000

------------------------------------------------------------
x$quiz: 2

        Welch Two Sample t-test

data:  percent by group
t = 1.591, df = 5.875, p-value = 0.1638
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -14.03856  65.46713
sample estimates:
mean in group High  mean in group Low
          65.71429           40.00000

------------------------------------------------------------
x$quiz: 3

        Welch Two Sample t-test

data:  percent by group
t = -0.1541, df = 3.506, p-value = 0.886
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -66.87207  60.20541
sample estimates:
mean in group High  mean in group Low
          71.66667           75.00000

>


On 10/22/07, Matthew Dubins <matt.dubins at utoronto.ca> wrote:
> Hi,
>
> Following please find *some* of my data.
>
> percent         quiz    group
> 100     1       High
> 100     1       High
> 100     1       High
> 25      1       Low
> 50      1       Low
> 75      1       High
> 50      1       Low
> 75      1       High
> 100     1       High
> 100     1       High
>
> 50      1       Low
> 60      2       High
> 20      2       High
> 100     2       High
> 60      2       Low
> 40      2       Low
> 80      2       High
> 20      2       Low
> 60      2       High
> 40      2       High
> 100     2       High
>
> 90      3       High
> 100     3       High
> 0       3       High
> 90      3       Low
> 80      3       High
> 60      3       Low
> 100     3       High
> 60      3       High
>
>
> Here is the by command i used:
>
> by(percent, quiz, function(percent) {t.test(percent~group,
> data=marks.long)})
>
> Basically it gave me three of the same t.tests based on the full data
> set, not the subsets (quiz 1, 2 and 3).
>
> So far it seems like my method is more straightforward!
>
>
>
> Julian Burgos wrote:
> > Could you post some of your data and your initial test, and explain
> > why it didn't worked?  It is difficult to figure out what is the
> > problem with your call to by().
> >
> > Julian
> >
> > Matthew Dubins wrote:
> >> I've tried to use by(), but the closest i got to it doing what I
> >> wanted was using the following:
> >>
> >> by(percent, quiz, function(percent) {t.test(percent~group,
> >> data=marks.long)})
> >>
> >> But the results it gave me weren't t.tests of percent by group
> >> according to quiz number.
> >>
> >>
> >> Julian Burgos wrote:
> >>> See by()
> >>>
> >>> Matthew Dubins wrote:
> >>>> Hi all,
> >>>>
> >>>> I wrote a simple function that gives me multiple t.test results
> >>>> according to a subset variable and am wondering whether or not I
> >>>> reinvented the wheel.  Observe:
> >>>>
> >>>> t.test.sub <- function (formula, data, sub, ...)
> >>>>     {
> >>>>         for(i in 1:max(sub))
> >>>>             {
> >>>>                 print(t.test(formula, data = subset(data, sub ==
> >>>> i), ...))
> >>>>             }
> >>>>     }
> >>>>
> >>>> Is there already a similar function in some package?
> >>>>
> >>>> Thanks,
> >>>> Matthew Dubins
> >>>>
> >>>> ______________________________________________
> >>>> 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.
> >>>
> >>
> >
>
>
>        [[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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list