0

It's my first couple of weeks using R and I've been breaking my head over a problem. I hope you guys can help me out!

I'm doing a multiway ANOVA (aov(Eicosane~SEX+STATUS,data=data_wide). Now I have about 26 different dependent variables, which are chemical compounds(in this example Eicosane) found on Drosophila suzukii fruit flies and I want to see if the chemical compound found can be addressed to sex or status. Is there a way to use some kind of loop so I don't need to change the dependent variable manually each time and can get the output in 1 file?

Someone recommended; {For (i in 1:26)}
(aov(i ~SEX+STATUS,data=data_wide)}
Result}

But it gives Error: unexpected 'in'. Does R automatically number my colums in my dataset as 1,2,3 etc... ?

I hope it's a bit clear and my apologies for this newbie question but I just can't seem to figure it out myself.

Thanks a lot!

Rist
  • 9
  • 1
  • 2
    Hi there, welcome to the community and R! May I suggest that you edit your question to include a reproducible example ("reprex")? That'll make it much easier to help you find an answer. Here are some sources on how to make a reprex, https://github.com/jennybc/reprex#reproducible-examples (another source: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Statwonk Dec 10 '15 at 17:57

1 Answers1

0

For example:

data(prostate, package= "faraway")
for (i in 1:ncol(prostate)) {
  print(summary(aov(prostate[,i] ~., data= prostate)))
}

In this case I use all columns vs the 1 remaining in the call to aov. If you wish to exclude SEX and STATUS from your set of dependent variables, you'll have to modify the call in the for loop, eg:

cols <- (1:ncol(data_wide))[-c(which(names(data_wide) %in% c("SEX", "STATUS")))]
for (i in cols) {...}
alexwhitworth
  • 4,839
  • 5
  • 32
  • 59