Search code examples
rloopsdataframet-test

T.test for each row in R


I am working with two data frames both with 20502 rows, and I want to preform a t.test with the 1st row of one data frame and the 1st of the other data frame and so one until the 20502 row of the two data frames.

ps: I have missing values (NA) in my data frames


Solution

  • If I'm understanding what you're doing correctly, this isn't the typical way to use data.frames - typically, columns should stand on their own as variables, not rows, and the host of data.frame tools available makes use of this assumption. That being said, for data.frames A and B below...

    A <- as.data.frame(matrix(1:12,nrow=3,ncol=4))
    A
    ##   V1 V2 V3 V4
    ## 1  1  4  7 10
    ## 2  2  5  8 11
    ## 3  3  6  9 12
    
    B <- as.data.frame(matrix(2:13,nrow=3,ncol=4))
    B
    ##   V1 V2 V3 V4
    ## 1  2  5  8 11
    ## 2  3  6  9 12
    ## 3  4  7 10 13
    

    ...what you'd like to do is a 2-sample t-test of c(1,4,7,10) vs. c(2,5,8,11), etc for each row, all the way down. You could get there with

    pvals <- rep(NA, nrow(A))
    for(i in 1:nrow(A)) pvals[i] <- t.test(A[i,],B[i,])$p.value