I want to write a code to perform anova where I don't have to change the code every time I change a dataset or when I change the variables for parsing. As my code is it is strict strict there is only one type of dataset. For example; Answer ~ Factor1 + factor2 for condition A but if I change the condition to B, C etc. I would like to initially parameterize to avoid hard codes. How can I make my code more functional and less rigid?
Example
input <- data.frame(
order = gl(2,50, label = c(paste('area', LETTERS[1:2]))),
f1 = gl(5,10, label = c(paste('conc', LETTERS[1:5]))),
f2 = gl(5,2, label = c(paste('plot', LETTERS[1:5]))),
f3 = factor(rep(paste("cond", 1:2, sep =""), 5)),
values = abs(rnorm(100))
)
model <- by(input,input$order, function(x){
f1 = levels(factor(x$f1))
f2 = levels(factor(x$f2))
f3 = levels(factor(x$f3))
order = levels(factor(x$order))
for( i in (1: length(f1))){
for(j in (1: length(order))){
di <- x[x$f1 == f1[i] & x$order == order[j] ,]
write(paste('\nf1:', "f1", f1[i],order[j],'\n'), stderr())
anova.1 <- aov(values ~ f2 * f3, di)
print(summary(anova.1))
}
}
write("Analyse Finished! \n", stderr())
})