HI Shantanu,
I saw your reply to Rui regarding multiple underscores in Nabble:
(Actually, I see now that part of the problem is that many of the
names have multiple underscores such as "red_apple_pre" or
"post_banana_organic". I think this is causing a problem for this line
in your code:)
I wasn't aware of that problem. In that case, try this:
set.seed(432)
dat2<-data.frame(red_apple_pre=sample(10:20,5,replace=TRUE),orange_post=sample(18:28,5,replace=TRUE),pre_banana_organic=sample(25:35,5,replace=TRUE),post_apple=sample(20:30,5,replace=TRUE),banana_post=sample(40:50,5,replace=TRUE),orange_pre=sample(5:10,5,replace=TRUE))
?nam1<-c("apple","orange","banana")
?nam2<-c("pre","post")
colnames(dat2)<-unlist(lapply(lapply(strsplit(colnames(dat2),"_"),function(x) x[x%in%nam1|x%in%nam2]),function(x) paste(x[1],x[2],sep="_")))
colnames(dat2)<-gsub("^pre\\_(.*)","\\1_pre",gsub("^post\\_(.*)","\\1_post",colnames(dat2)))
dat3<-t(dat2[order(colnames(dat2))])
dat3<-data.frame(varName=gsub("(.*)\\_.*","\\1",row.names(dat3)),dat3)
list3<-lapply(split(dat3,dat3$varName),function(x) t(x[-1]))
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)))
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
I hope this works.
A.K.
----- Original Message -----
From: "Nundy, Shantanu" <snundy at chicagobooth.edu>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Thursday, October 11, 2012 10:22 AM
Subject: RE: [R] multiple t-tests across similar variable names
hi Arun,
This is very helpful thanks.
I'm running into a couple issues:
1. Since some of the variables start with "pre_apple" and others "apple_post" sorting the variables doesn't completely put pre-post variables next to each other.
2. I have about 50 variables so typing this line is a bit cumbersome:
list3<-list(dat3[,1:2],dat3[,3:4],dat3[,5:6])
Thanks, Shantanu
From: arun [smartpink111 at yahoo.com]
Sent: Thursday, October 11, 2012 9:14 AM
To: Rui Barradas
Cc: Nundy, Shantanu; R help
Subject: Re: [R] multiple t-tests across similar variable names
Sent: Thursday, October 11, 2012 9:14 AM
To: Rui Barradas
Cc: Nundy, Shantanu; R help
Subject: Re: [R] multiple t-tests across similar variable names
HI Rui, By running your code, I got the results as: result #? ? ? MeanDiff? CIlower? ? CIupper? ? ? p.value #apple? ? -12.6 -16.68052? -8.519476 0.0010166626 #banana? ? -15.0 -17.91196 -12.088040 0.0001388506 #orange? ? -18.2 -22.79583 -13.604166 0.0003888560