0

I am trying to run multiple t-tests between column data read from two separate files. The two data frames look like:

df1 = A    C    D
      1    2    3
      4    5    6
      7    8    9

df2 = A    B    E
      10   11   12
      13   14   15 
      16   17   18

I would like to run t-tests between columns with the same headers. For the data frames above, I should only get one t-test result for A. What is the best approach for this?

Jozin
  • 25
  • 3
  • 4
    Try `Map(t.test, df1[nm1], df2[nm1])` where `nm1 <- intersect(names(df1), names(df2))` – akrun Aug 27 '19 at 15:57

1 Answers1

3

You could do:

t.test.col <- names(df1)[names(df1) %in% names(df2)]

  for (i in 1:length(t.test.col)){
   print(paste0("Doing t.test for: ", t.test.col[i]))
   print(t.test(df1[t.test.col][i], df2[t.test.col][i]))
  }
MAPK
  • 5,635
  • 4
  • 37
  • 88