Skip to content

t.test in a loop

3 messages · Michael Pearmain, Thomas Lumley, PIKAL Petr

#
On Wed, 28 Jan 2009, Michael Pearmain wrote:

            
There's the additional problem that if your code worked it would do 16 t-tests but only report the last one.

Assuming you want them printed

for(v in names(channel.data.train)[1:16]) {
   print(v)
   print(t.test(channel.data.train[[v]]~channel.data.train$power)
}

or
for(v in names(channel.data.train)[1:16]){
   test <- eval(bquote(.(v)~power, data=channel.data.train)
   print(eval(test))
}

This sort of use of eval is fairly harmless.

        -thomas
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
Hi 

r-help-bounces at r-project.org napsal dne 28.01.2009 12:57:55:
a
of the
t-tests
Another option is to use lapply

lapply(channel.data.train[, 1:16], function(x) 
t.test((x)~channel.data.train$power)

Regards
Petr
t.test(get(paste("channel.data.train$",i,"~channel.data.train$power",sep="")))
"~channel.data.train$power",
found
to
please
sender
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html