[R] For loop for subset - repeating same over and over?

Dennis Murphy djmuser at gmail.com
Fri Sep 30 12:14:27 CEST 2011


Hi:

This would be a lot easier to check with a reproducible example, but
here's a simplified version of your problem:

testd <- data.frame(gps = rep(c("ADHALP","ADLCON","ADLARC","BDALAT","BDPARC"),
                              each = 15),
                    trt = rep(LETTERS[1:3], each = 5),
                      y = rnorm(75))
testd$gps <- as.character(testd$gps)
str(testd)

library('plyr')
# The subset is handled by the grouping variable gps:
mlist <- dlply(testd, .(gps), function(d) aov(y ~ trt, data = d))
# Apply the summary function to each groupwise ANOVA:
llply(mlist, summary)

# Equivalent approach:
# splitting the data by gps is equivalent to use of the grouping
variable in dlply()
mlist2 <- lapply(split(testd, testd$gps), function(d) aov(y ~ trt, data = d))
llply(mlist2, summary)

Is that what you had in mind?

Dennis

On Thu, Sep 29, 2011 at 4:37 PM, kelseyann <kremerke at mail.gvsu.edu> wrote:
> Hello, I am using the following script to run an anova for numerous species
> in a table that I have:
>
>
> SiteSpp <-
> c("ADHALP","ADLCON","ADLARC","BDALAT","BDPARC","BDLCON","BDLARC","AWCAQU","AWERUS","AWEANG","AWDPSI","BWCSTA","BWHPAU","BWETRI","BWERUS","BWDFIS","BWPARC","BWLCON","BWLARC","BWJBIG")
>
> n.SiteSpp <- length(SiteSpp)
>
>  for (i in (1:n.SiteSpp)) {
>      QInfl.aov <- aov(AvgOfnumResponse ~ strTrea*strYear,
> data=QInflAvgbyPlot, subset=(SiteSpp == SiteSpp[i]))
>      print(summary(QInfl.aov))
>  }
>
>
> The resulting summary just continuously prints the same anova results (For
> ADHALP) over and over again.... Any suggestions?
>
> Thanks,
> Kelsey
>
> --
> View this message in context: http://r.789695.n4.nabble.com/For-loop-for-subset-repeating-same-over-and-over-tp3858115p3858115.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list