0

I want to run an ANOVA test on several data (Day, Week and Month). My idea was to create a loop that call each dataset (Day, Week and Month). However the following code does not work, because does not recognise the value[i].

df1<-data.frame(Day=c(1,2,5,6,9,8),Week=c(22,45,33,3,2,11),Month=c(45,6,7,12,4,7),
           type=c("A","C","B","A","C","B"))

value<-c("Day","Week","Month")

    for(i in 1:length(value)){
    av<-aov(value[i] ~ type, data = data)
    return(av)
    }

Do you have any suggestion on how I could improve the code?

2
  • 1
    Try get(value[i]). Or, maybe better, df1[[ value[i] ]]. Commented Aug 26, 2017 at 15:10
  • This is what I needed. Thanks. Commented Aug 26, 2017 at 17:48

1 Answer 1

2

You don't need a loop:

m <- aov(cbind(Day, Week, Month) ~ type, data = df1)
summary(m)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.