Well, you never actually made an attempt to use the i
variable that you are looping over. I went ahead and changed the for
loop to use lapply
instead. How about this
res<-lapply(levels(data$metabolite), function(i) {
met<-data[data$metabolite==i, ] #subset for metabolite i
part1<-with(met, aov(area ~ treatment))
print(part1)
list(
part1 = part1,
part2 = TukeyHSD(x=part1, 'treatment', conf.level=0.95)
)
})
names(res)<-levels(data$metabolite)
This will give you a list or lists that contain the parts you're looking for. For example you can extract results with res[[_metabolite_]][[_part_]]
so for example...
res[["x"]][["part1"]]
res[["y"]][["part2"]]