1

I have over 150 t-tests between different groups in a study (with only like 25 variables being significant) but I still need to list out all the results for my thesis appendix.

Is there an easy way to combine each of the t-tests into 1 table?

Also, I know my method is inefficient, I couldn't figure out the for loop (if anyone would know how to do that with 2 databases)

Example of the t-tests.

t.test(Q8d3landowner$Q8_1, Q8d3rent$Q8_1, var.equal = TRUE)
t.test(Q8d3landowner$Q8_2, Q8d3rent$Q8_2, var.equal = TRUE)
t.test(Q8d3landowner$Q8_3, Q8d3rent$Q8_3, var.equal = TRUE)
t.test(Q8d3landowner$Q8_4, Q8d3rent$Q8_4, var.equal = TRUE)
t.test(Q8d3landowner$Q8_5, Q8d3rent$Q8_5, var.equal = TRUE)
t.test(Q8d3landowner$Q8_6, Q8d3rent$Q8_6, var.equal = TRUE)
t.test(Q8d3landowner$Q8_7, Q8d3rent$Q8_7, var.equal = TRUE)
t.test(Q8d3landowner$Q8_8, Q8d3rent$Q8_8, var.equal = TRUE)

t.test(Q8d3own$Q8_1, Q8d3rent$Q8_1, var.equal = TRUE)
t.test(Q8d3own$Q8_2, Q8d3rent$Q8_2, var.equal = TRUE)
t.test(Q8d3own$Q8_3, Q8d3rent$Q8_3, var.equal = TRUE)
t.test(Q8d3own$Q8_4, Q8d3rent$Q8_4, var.equal = TRUE)
t.test(Q8d3own$Q8_5, Q8d3rent$Q8_5, var.equal = TRUE)
t.test(Q8d3own$Q8_6, Q8d3rent$Q8_6, var.equal = TRUE)
t.test(Q8d3own$Q8_7, Q8d3rent$Q8_7, var.equal = TRUE)
t.test(Q8d3own$Q8_8, Q8d3rent$Q8_8, var.equal = TRUE)

Output example

Two Sample t-test

data:  Q8d3own$Q8_5 and Q8d3rent$Q8_5
t = 1.3478, df = 943, p-value = 0.178
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.04012354  0.21607738
sample estimates:
mean of x mean of y 
 2.884892  2.796915 

2 Answers2

1

That's rather easy. Look into the result of one t.test,

tt <- t.test(Q1$X1, Q3$X1)
str(tt)  ## displays structure

or at the names

names(tt)
# [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
# [6] "null.value"  "stderr"      "alternative" "method"      "data.name" 

to decide what to use, i.e. on what to subset the result.

Then you can use one-liners with mapply.

t(mapply(\(...) t.test(..., var.equal=TRUE)[c('statistic', 'p.value')], Q1, Q3))
#     statistic  p.value   
# X1  0.4672679  0.640822  
# X2  -0.3376005 0.7360219 
# X3  -0.413335  0.6798085 
# X4  -0.3215545 0.7481294 
# X5  -1.005416  0.3159237 
# X6  -0.6543237 0.5136626 
# X7  -0.9552577 0.3406122 
# X8  -1.003932  0.3166368 
# X9  0.3103211  0.7566432 
# X10 1.920206   0.05626892

t(mapply(\(...) t.test(..., var.equal=TRUE)[c('statistic', 'p.value')], Q2, Q3))
#     statistic  p.value   
# X1  -0.6516574 0.5153779 
# X2  0.5773907  0.5643315 
# X3  0.1885351  0.8506503 
# X4  -1.724851  0.08611486
# X5  -0.5244006 0.6005865 
# X6  -0.6333486 0.5272369 
# X7  1.493958   0.1367784 
# X8  -0.5299757 0.5967226 
# X9  0.9243501  0.3564292 
# X10 0.4313086  0.6667132 

Data:

set.seed(42)
m <- 100; n <- 10
Q1 <- data.frame(matrix(rnorm(m*n), m, n))
Q2 <- data.frame(matrix(rnorm(m*n), m, n))
Q3 <- data.frame(matrix(rnorm(m*n), m, n))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    This worked! Thank you. Ill add for future reference, if you get the following error, make sure you don't have any columns for labeling. "Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : missing value where TRUE/FALSE needed" – Corrin Winter Mar 12 '23 at 23:47
0

You did not give us an reproducible example, so I will use some simulated data. You need to organize your data so you can use some form of loop:

test1 <- as.data.frame(matrix(as.numeric(NA), 15, 20))
test2 <- as.data.frame(matrix(as.numeric(NA), 15, 20))
  
for (i in 1:20) test1[, i] <- rnorm(15, 0, 1)
for (i in 1:20) test2[, i] <- rnorm(15, 0.5, 1)

# A function doing the t.test and collecting the results you want in the table

my.test <- function(x, y) { 
  l <- t.test(x, y, var.equal=TRUE)
  return(list(l$statistic, l$p.value)) # add the others you want in your table
}

res <- matrix(as.numeric(NA), 20, 2)

colnames(res) <- c("statistic", "p.value")

for (i in 1:20) res[i, ] <- unlist(my.test(test1[, i], test2[, i])) 

The matrix res now contains the results, and I leave the formatting for you.

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28