1

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.

Community
  • 1
  • 1
rasec
  • 13
  • 3
  • 1
    It's not clear to me what the desired result is. Wouldn't be `names(tests1) <- apply(matrix(names(df), nrow=2), 2, paste, collapse=" vs ")` sufficient? – Roland Aug 28 '14 at 17:51
  • @Roland That's one solution. At minimum I would like to see the variable names for the relevant t test in the output. I wondered also, however, if there might be another approach that called the variables by their names rather than their index. Thanks. – rasec Aug 28 '14 at 19:04

1 Answers1

1

You can split the dataframe and then use mapply:

    col<-seq(1,ncol(df),by=2)
    mapply(t.test,df[,col],df[,-col],MoreArgs=list(paired=TRUE))

In this way the names of the resulting list will be the names of the odd coulmns of df.

nicola
  • 24,005
  • 3
  • 35
  • 56