HI, If you have a lot of variables and in no order, then it would be better to order the data by column names. For e.g. set.seed(432) dat2<-data.frame(apple_pre=sample(10:20,5,replace=TRUE),orange_post=sample(18:28,5,replace=TRUE),banana_pre=sample(25:35,5,replace=TRUE),apple_post=sample(20:30,5,replace=TRUE),banana_post=sample(40:50,5,replace=TRUE),orange_pre=sample(5:10,5,replace=TRUE)) dat3<-dat2[order(colnames(dat2))] #order the columns list3<-list(dat3[,1:2],dat3[,3:4],dat3[,5:6]) res3<-do.call(rbind,lapply(lapply(list3,function(x) t.test(x[,1],x[,2],paired=TRUE)),function(x) data.frame(meandifference=x$estimate,CIlow=unlist(x$conf.int)[1],CIhigh=unlist(x$conf.int)[2],p.value=x$p.value))) row.names(res3)<-unlist(unique(lapply(strsplit(colnames(dat3),"_"),`[`,1))) res3 # meandifference CIlow CIhigh p.value #apple 12.6 8.519476 16.68052 0.0010166626 #banana 15.0 12.088040 17.91196 0.0001388506 #orange 18.2 13.604166 22.79583 0.0003888560
A.K. ----- Original Message ----- From: "Nundy, Shantanu" <snu...@chicagobooth.edu> To: "r-help@r-project.org" <r-help@r-project.org> Cc: Sent: Wednesday, October 10, 2012 7:09 PM Subject: Re: [R] multiple t-tests across similar variable names Hi everyone- I have a dataset with multiple "pre" and "post" variables I want to compare. The variables are named "apple_pre" or "pre_banana" with the corresponding post variables named "apple_post" or "post_banana". The variables are in no particular order. apple_pre orange_pre orange_post pre_banana apple_post post_banana person_1 person_2 person_3 ... person_x How do I: 1. Run a series of paired t-tests for the apple_pre variables and pre_banana variables? Would be great to do something like ttest(*.*pre*.*,*.*post*.*). 2. Print the results from these t-tests in a table with col 1=mean difference, col 2= 95% conf interval, col 3=p-value. Thank you kindly, -Shantanu Shantanu Nundy, M.D. University of Chicago [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.