Re: [R] getting t.test to work with apply()

From: Andrew Yee <andrewjyee_at_gmail.com>
Date: Mon, 04 Jun 2007 09:51:36 -0400

Thanks.

It turns out that the data is arranged with the rows as the variables and the samples as the columns. It has 10 rows.

Furthermore, when I removed the commas,

results <-apply(raw.sample,1,function(x) t.test(x[alive],x[dead])) I get the following error message:

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") :

        missing value where TRUE/FALSE needed In addition: Warning messages:
1: argument is not numeric or logical: returning NA in: mean.default(x) 2: argument is not numeric or logical: returning NA in: mean.default(y)
>

I think in the end, the lapply suggestion works the best!

Andrew

On 6/4/07, Peter Dalgaard <P.Dalgaard_at_biostat.ku.dk> wrote:
>
> Andrew Yee wrote:
> > Thanks for everyone's suggestions.
> >
> > I did try
> >
> > results <-apply(raw.sample,1,function(x) t.test(x[,alive],x[,dead]))
> >
> > However, I get:
> >
> > "Error in x[, alive] : incorrect number of dimensions"
> >
> > Full disclosure, raw.sample is a data.frame, and I am using alive and
> dead
> > as indexing vectors.
> >
> > On the other hand, the lapply suggestion works better.
> >
> > results <- lapply(1:nrow(raw.sample), function(i) t.test(raw.sample
> > [i,alive],raw.sample[i,dead]))
> >
> >
> nrow()?
>
> Oops, yes. I didn't notice that your data are transposed relative to the
> usual "cases x variables" layout.
>
> So mapply() is not going to work unless you use
> as.data.frame(t(raw.sample)) first.
>
> -pd
> > Thanks,
> > Andrew
> >
> >
> > On 6/4/07, Peter Dalgaard <P.Dalgaard_at_biostat.ku.dk> wrote:
> >
> >
> >> Petr Klasterecky wrote:
> >>
> >>> Andrew Yee napsal(a):
> >>>
> >>>
> >>>> Hi, I'm interested in using apply() with t.test() on a data.frame.
> >>>>
> >>>> Specifically, I'd like to use apply() to do the following:
> >>>>
> >>>> t.test(raw.sample[1,alive],raw.sample[1,dead])
> >>>> t.test(raw.sample[2,alive],raw.sample[2,dead])
> >>>> t.test(raw.sample[3,alive],raw.sample[3,dead])
> >>>> etc.
> >>>>
> >>>> I tried the following,
> >>>>
> >>>> apply(raw.sample,1,function(x) t.test(raw.sample[,alive],raw.sample
> >>>>
> >> [,dead]))
> >>
> >>> Two comments:
> >>> 1) apply() works on arrays. If your dataframe only has numeric values,
> >>> turn it (or its copy) to a matrix via as.matrix(). If it has mixed
> >>> variables, take only the numeric part for t-tests. The conversion is
> >>> made implicitly but explicit asking for it cannot hurt.
> >>> 2) the main problem - you are using a wrong argument to t.test
> >>>
> >>> The call should look like
> >>> apply(as.matrix(raw.sample), 1, function(x){t.test(x[alive],
> x[dead])})
> >>>
> >>> assuming 'alive' and 'dead' are logical vectors of the same length as
> >>>
> >> 'x'.
> >>
> >>> Petr
> >>>
> >>>
> >> Notice also that the other apply-style functions may give an easier
> >> route to the goal:
> >>
> >> lapply(1:N, function(i) t.test(raw.sample[i,alive],raw.sample[i,dead]))
> >>
> >> or (maybe, depends on raw.sample being a data frame and alive/dead
> being
> >> indexing vectors)
> >>
> >> mapply(t.test, raw.sample[,alive], raw.sample[,dead])
> >>
> >>
> >>>> but it gives me a list of identical results.
> >>>>
> >>>>
> >>>> Thanks,
> >>>> Andrew
> >>>>
> >>>> [[alternative HTML version deleted]]
> >>>>
> >>>> ______________________________________________
> >>>> R-help_at_stat.math.ethz.ch 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.
> >>>>
> >>>>
> >>>>
> >>>
> >> --
> >> O__ ---- Peter Dalgaard �ster Farimagsgade 5, Entr.B
> >> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> >> (*) \(*) -- University of Copenhagen Denmark Ph: (+45)
> >> 35327918
> >> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45)
> >> 35327907
> >>
> >>
> >>
> >
> > [[alternative HTML version deleted]]
> >
> >
> > ------------------------------------------------------------------------
> >
> > ______________________________________________
> > R-help_at_stat.math.ethz.ch 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.
> >
>
>
> --
> O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45)
> 35327918
> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45)
> 35327907
>
>

        [[alternative HTML version deleted]]



R-help_at_stat.math.ethz.ch 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. Received on Mon 04 Jun 2007 - 14:08:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Jun 2007 - 16:31:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.