I am new to R and programming. I am trying to apply a function (paired t test) on sequential pairs of columns and would like the output to use the column names rather than indices. I have a data.frame with 100 rows and 60 columns. As a smaller example, assume the following dataset:
df <- data.frame(a1=rnorm(100, mean=60, sd=9),
a2=rnorm(100, mean=60, sd=9),
b1=rnorm(100, mean=65, sd=8),
b2=rnorm(100, mean=65, sd=8),
c1=rnorm(100, mean=75, sd=15),
c2=rnorm(100, mean=70, sd=15),
d1=rnorm(100, mean=75, sd=12),
d2=rnorm(100, mean=70, sd=12))
I have managed to run the t tests, however the following code (from an answer to another question https://stackoverflow.com/a/9661591) uses the variable indices to report results:
tests1 <- lapply(seq(1,ncol(df), by=2),
function (x){t.test(df[,x],df[,x+1],paired=TRUE)})
print(tests1)
I have tried the following using sapply, but it does not provide the column names in the output:
tests2 <- sapply(seq(1,ncol(df), by=2),
function (x){t.test(df[,x],df[,x+1])},
simplify=FALSE,
USE.NAMES=TRUE)
print(tests2)
I would appreciate any help and suggestions.