sertsedat
sertsedat

Reputation: 3600

How to use 2 columns in one function(t.test) after group by

I have a list where I need to use t.test function over 2 columns. I know I can just do

t.test(myF$before, myF$after)

but after grouping I have many groups and it would be a hassle to do all one by one.

I get my data with SQL Query such as below:

myF <- sqlQuery(conn, 'Select colA, colB, colC, before, after from MyTable')

following code will give me mean over before and after columns one by one

aggregate(cbind(before = before, after = after) ~ colA+colB+colC, 
          data = myF, 
          FUN = mean)

Output is similar to this:

colA      colB     colC      before after
GroupA1   GroupB1  GroupC1   110    11
GroupA2   GroupB1  GroupC1   44     15
GroupA3 .....................
......

So I want to get t.test results between before and after columns for each group. How can I achieve that?

Upvotes: 1

Views: 36

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48241

You didn't provide a clear input or expected output, but it looks like, e.g.,

myF %>% group_by(colA, colB, colC) %>% summarise(pval = t.test(.$before, .$after)$p.val)

should achieve what you want.

Upvotes: 1

Related Questions