Jaafa
Jaafa

Reputation: 141

How to run t-test on pairs of columns in dataframe in R?

I have dataframe with 6 columns of numerical data. I want to run a t-test on pairs of columns in the form; the first + second column, third+fourth column, fifth + sixth column, until I have 3 t-test p-values which I can store in a new dataframe.

I know I can get the columns with select() and I can extract the p-values with t.test(x,y)$p.values. But I can't figure out a good way of automating this in a for loop.

I thought of doing something like this:

p-values <- for (i in select(dataframe,-1) t.test(i,?)$p.value

As a start but not sure where to go from here.

Upvotes: 0

Views: 87

Answers (2)

David O
David O

Reputation: 803

Creating an index of columns as Rémi Coulaud did is a good approach. Here is a variation that creates the same index and returns the paired p-values directly:

  df <- as.data.frame(matrix(rnorm(36), nrow = 6, ncol = 6))
  indices <- matrix(seq_len(ncol(df)), nrow = 2)
  p.values <- apply(indices, 2, function(idx) t.test(df[idx])$p.value)

Upvotes: 1

R&#233;mi Coulaud
R&#233;mi Coulaud

Reputation: 1714

A quick answer will be :

df <- matrix(rnorm(18), nrow = 3, ncol = 6)
p <- ncol(df)
indices <- cbind(seq(1, p, 2), seq(2, p + 1, 2))

for(i in 1:(p/2)){
  print(t.test(df[,indices[i,]])$p.value)
}

It works only if you have a number pair of columns.

Upvotes: 1

Related Questions