Search code examples
rsyntaxt-test

R t.test() with data.frames as arguments


There is a question on CrossValidated, where someone gave two dataframes instead of two vectors to the t.test function: https://stats.stackexchange.com/questions/261830/t-test-or-wilcox-in-r-and-how-to-apply-to-dataframe-splitted-in-2-groups/

See this code for a shorter example

a <- data.frame(foo=1:5, bar=5:9)
b <- data.frame(foo=1:5, bar=5:9)
t.test(a,b)

The help page for the t.test function clearly states that x and y should be

a (non-empty) numeric vector of data values.

Still the above code throws no error but gives a result. What is the meaning of the result?


Solution

  • This is undocumented behavior, but you go against documentation when passing data.frames.

    This happens:

    x <- a
    y <- b
    yok <- !is.na(y)
    xok <- !is.na(x)
    y <- y[yok]
    #[1] 1 2 3 4 5 5 6 7 8 9
    x <- x[yok]
    #[1] 1 2 3 4 5 5 6 7 8 9
    

    Basically, you get the same result as if you did t.test(unlist(a), unlist(b)).