0

I'm attempting to do a T-Test across many different columns at once, but can't figure out how to do it. Here is what I have for one of the columns, Clout.

t.test(wf[wf$ThreadID == 249001,]$Clout,wf[wf$ThreadID == 230005,]$Clout)

But I have about 20 other columns, e.g. Authentic, Tone

How can I run this test across all columns?

Sam
  • 6,616
  • 8
  • 35
  • 64

1 Answers1

2

Try the following.

n <- ncol(wf)
inx1 <- which(wf$ThreadID == 249001)
inx2 <- which(wf$ThreadID == 230005)
ttest_list <- lapply(seq_len(n), function(j) t.test(wf[inx1, j], wf[inx2, j]))

Note: How can you be sure that there are as many elements such that wf$ThreadID == 249001 and wf$ThreadID == 230005?

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • I've ended up using `sapply(c("Clout","Tone","Authentic"), function(j) t.test(wf[inx1, j], wf[inx2, j]))` – Sam Sep 10 '17 at 06:33
  • If those are the only columns you want, then yes, it seems it works. Sorry if I misunderstood the question. As for the use of `sapply`, note that it will make no difference, the resulting list is exactly the same. – Rui Barradas Sep 10 '17 at 06:42
  • I wanted all the columns but at least one of the columns wasn't working so it all didn't run. – Sam Sep 11 '17 at 04:03
  • @Sam What error did it throw? Are all the columns numeric? – Rui Barradas Sep 11 '17 at 06:59