基于列 headers 的条件 t-test

Conditional t-test based on column headers

我正在尝试 运行 多个 t-tests 从两个单独的文件读取的列数据。两个数据框看起来像:

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

我想 运行 t-tests 列之间具有相同的 headers。对于上面的数据框,对于 A,我应该只得到一个 t-test 结果。最好的方法是什么?

你可以这样做:

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]))
  }