[R] apply, t-test and p-values PREVIOUS SUGGESTION DID NOT WORK

Gábor Csárdi csardi.gabor at gmail.com
Thu Oct 16 18:18:28 CEST 2008


How about the following?

sapply(1:nrow(sample1), function(x) t.test(sample1[x,], sample2[x,])$p.value)

Gabor

On Thu, Oct 16, 2008 at 6:07 PM, John Sorkin
<jsorkin at grecc.umaryland.edu> wrote:
> Although I am grateful to Jorge and David for their suggestions, their solution will not solve my problem as sample2 is not fixed. Let me demonstrate what I want to do:
>
>
> # Define two matricies
> sample1<-matrix(data=c(1:20),byrow=TRUE,ncol=5,nrow=4)
> sample2<-sample1+rnorm(20)
>
> These are the computations that I would like to perform using apply (or some similar method):
>
>
> # Compute t-test comparing first row of sample 1 to first row of sample 2
> t.test(sample1[1,],sample2[1,])
>
> # Compute t-test comparing second row of sample 1 to second row of sample 2
> t.test(sample1[2,],sample2[2,])
>
> # Compute t-test comparing third row of sample 1 to third row of sample 2
> t.test(sample1[3,],sample2[3,])
>
> # Compute t-test comparing fourth row of sample 1 to fourth row of sample 2
> t.test(sample1[4,],sample2[4,])
>
>
>
> As you can see, the code below does not return the same values! It does not work.
> apply(sample1,1,function(x) t.test(x,sample1)$p.value)
>
> Sugestions would be appreciated!
> John
>
[...]
-- 
Gabor Csardi <Gabor.Csardi at unil.ch>     UNIL DGM



More information about the R-help mailing list