On Fri, May 2, 2008 at 10:19 AM, David Schwab <dvschwab46225_at_gmail.com>
wrote:

> I am writing a simple R program to execute a t-test repeatedly on data
> contained in a data frame. My data looks like this:
>
>
>
> Category Value1 Value2
>
> 1 .5 .8
>
> 1 .3 .9
>
> . . . . . . . . .
>
> 2 1.4 1.3
>
> 2 1.3 1.3
>
> . . . . . . . . .
>
> 15 .2 .3
>
> 15 .5 .1
>
>
>
>
>
> So in all there are 15 categories, and each category contains two sets of
> observations which I want to compare. I only want to compare Value1 and
> Value2 within each category, but I need to do it 15 times (once for each
> category), so I wanted to write an R function to make it easier.
>
>
>
> Right now I am using a for() loop to do the comparison. My loop looks
> like
> this:
>
>
>
> for(i in 1:21)
>
> {
>
> x <- t.test(Value1[Category == i], Value2[Category == i])
>
> y <- c(y, x$p.value)
>
> }
>
>
>
> The loop runs and everything is working well. However, I am not sure how
> to
> translate this code into a function. In particular, I'm not sure how to
> write a function that passes a data frame ds (containing Category, Value1,
> and Value2 as members) as an argument, and then accessing these members
> within the body of the function. I've tried the following:
>
>
>
> repeated_test <- function(data)
>
> {
>
> for(i in 1:21)
>
> {
>
> x <- t.test(ds$Value1[ds$Category == i],
> ds$Value2[ds$Category
> == i])
>
> y <- c(y, x$p.value)
>
> }
>
>
>
> This will run, but only if the members of the data frame I am passing as
> an
> argument are in fact named Value1, Value2, and Category. This is fine for
> now, but in the future I will have to run this function on data where I
> cannot be sure this is the case. Rather than change the member names by
> hand, I would like to make the function generic to work with any data
> frame.
> How do I do this? Or is there a better way to do this without the for()
> loop (for example, using apply())?
>
> [[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.
>