To follow up on my previous post,

1. I eliminated the non-numeric columns in the data.frame.  I didn't
appreciate that for apply to work, it really has to be a matrix from the get
go.
2. and eliminated the commas with x[,alive] so that it looks like apply(
raw.sample, 1, function(x) t.test(x[alive], x[dead])).  In other words,
function(x) t.test(x[,alive],x[,dead]) does not work.

With these steps, apply works.

Andrew


On 6/4/07, Petr Klasterecky <klaster@karlin.mff.cuni.cz> 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
>
> >
> > but it gives me a list of identical results.
> >
> >
> > Thanks,
> > Andrew
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@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.
> >
>
> --
> Petr Klasterecky
> Dept. of Probability and Statistics
> Charles University in Prague
> Czech Republic
>

	[[alternative HTML version deleted]]

