Skip to content
Prev 168443 / 398503 Next

t.test in a loop

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