Search code examples
rloopsfor-looplatext-test

Multiple t.test between one column and different columns in a table


I've tried to do a loop for a t-test between different columns, with one in particular and different ones. I would like to do a multiple t.test and insert it in a table to summarize the p-values and show with which columns I did the t.test.

I tried this,

    var1<- c('empfte','empft_rate','wage_st','wage_min','pmeal','hrsopen','bonus')
for (i in var1){
  result=t.test(eval(parse(text = paste0(i,"~state"))),data)
pvalue<-  print(i)
  print(result$p.value)
}

But that's not esthetic with "print".

Moreover, if it is possible to put the table in Latex format.


Solution

  • You could use lapply ("list-apply") to evaluate each variable against a single variable (in this case, "empfte" vs all others), e.g.

    library(tidyverse)
    var1 <- tribble(~'empfte',~'empft_rate',~'wage_st',~'wage_min',~'pmeal',~'hrsopen',~'bonus',
                      3, 3, 0.7, 22, 2.0, 8, 5,
                      4, 1, 0.7, 22, 2.5, 9, 5.1,
                      2, 1, 0.6, 22, 2.1, 2, 5.2,
                      3, 6, 0.8, 22, 2.9, 5, 5.3,
                      3, 6, 0.8, 22, 2.9, 5, 5.4,
                      3, 5, 0.8, 22, 2.9, 5, 5.8)
    
    t.test_func <- function(name){
      test <- t.test(x = var1$empfte, y = name)
      return(test$p.value)
    }
    
    list_of_results <- lapply(var1, t.test_func)
    table <- t(data.frame("empfte vs" = list_of_results))
    colnames(table) <- c("P-Value")
    table
    
                                       P-Value
        empfte.vs.empfte     1.000000000000000
        empfte.vs.empft_rate 0.526440549147148
        empfte.vs.wage_st    0.000280122578855
        empfte.vs.wage_min   0.000000008779076
        empfte.vs.pmeal      0.181259032114088
        empfte.vs.hrsopen    0.047201925878887
        empfte.vs.bonus      0.000087211443709
    
    library(xtable)
    xtable(t(data.frame("empfte vs" = list_of_results)))
    
        % latex table generated in R 4.0.3 by xtable 1.8-4 package
        % Tue Dec 22 11:44:20 2020
        \begin{table}[ht]
        \centering
        \begin{tabular}{rr}
          \hline
         & x \\ 
          \hline
        empfte.vs.empfte & 1.00 \\ 
          empfte.vs.empft\_rate & 0.53 \\ 
          empfte.vs.wage\_st & 0.00 \\ 
          empfte.vs.wage\_min & 0.00 \\ 
          empfte.vs.pmeal & 0.18 \\ 
          empfte.vs.hrsopen & 0.05 \\ 
          empfte.vs.bonus & 0.00 \\ 
           \hline
        \end{tabular}
        \end{table}
    
    

    You should be aware that these values do not correct for multiple testing (e.g. Bonferroni Correction) and an ANOVA is likely a more suitable statistical test.