[R] store results from loop into a dataframe

Sarah Goslee sarah.goslee at gmail.com
Tue Jan 5 18:19:46 CET 2016


Leaving aside the question of whether this is the best way to approach
your problem (unlikely), there's a couple of errors in your code
involving indexing. Once fixed, the code demonstrates some errors in
your use of HSD.test that will be harder for you to deal with.

Thanks for the complete reproducible example.

fun2 <- function (x)

{

    trait_names <- c("yield", "lp", "lnth")

    d = data.frame(yield = rep(0, 6), lp = rep(0, 6), lnth = rep(0,

        6))

    for (i in trait_names) {
# your formula has all the trait names, not the selected one
        # mod <- aov(formula(paste(trait_names, "~ PEDIGREE + FIELD +
PEDIGREE*FIELD + FIELD%in%REP")), data = x)
        mod <- aov(formula(paste(i, "~ PEDIGREE + FIELD +
PEDIGREE*FIELD + FIELD%in%REP")), data = x)

        out <- HSD.test(mod, "PEDIGREE", group = TRUE, console = FALSE)

# you're indexing by the trait name, instead of its position
        # d[, i] <- out$means[, 1]
        d[, which(trait_names == i)] <- out$means[, 1]

    }

    d

}

Sarah

On Tue, Jan 5, 2016 at 11:48 AM, DIGHE, NILESH [AG/2362]
<nilesh.dighe at monsanto.com> wrote:
> Dear R users:
>
> I am trying to create a function that will loop over three dependent variables in my aov model, and then get the HSD.test for each variable.  I like to store the results from each loop in a data frame.
>
>
>
> When I run my function (funx) on my data (dat), results from only yield gets populated in all three columns of the dataframe.  I am not able to store the results for each variable in a dataframe. Any help will be highly appreciated.
>
>
>
>
>
>
>
> function (x)
>
> {
>
>     trait_names <- c("yield", "lp", "lnth")
>
>     d = data.frame(yield = rep(0, 6), lp = rep(0, 6), lnth = rep(0,
>
>         6))
>
>     for (i in trait_names) {
>
>         mod <- aov(formula(paste(trait_names, "~ PEDIGREE + FIELD + PEDIGREE*FIELD + FIELD%in%REP")),
>
>             data = x)
>
>         out <- HSD.test(mod, "PEDIGREE", group = TRUE, console = FALSE)
>
>         d[, i] <- out$means[, 1]
>
>     }
>
>     d
>
> }
>
>
> structure(list(FIELD = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1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, 6L, 6L, 1L, 1L,
> 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L,
> 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L,
> 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L,
> 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L,
> 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
> 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("A", "B", "C", "D",
> "E", "F"), class = "factor"), yield = c(1003L, 923L, 1268L, 1226L,
> 1059L, 1150L, 900L, 816L, 1072L, 1158L, 1026L, 1299L, 1083L,
> 1038L, 1236L, 1287L, 1270L, 1612L, 1513L, 1676L, 1504L, 1417L,
> 1932L, 1644L, 1293L, 1542L, 1452L, 1180L, 1248L, 1764L, 1326L,
> 1877L, 1788L, 1606L, 1809L, 1791L, 2294L, 2315L, 2320L, 2083L,
> 1895L, 2284L, 2000L, 2380L, 1952L, 2414L, 2354L, 2095L, 2227L,
> 2093L, 2019L, 2505L, 2410L, 2287L, 2507L, 2507L, 2349L, 2162L,
> 2108L, 2319L, 2028L, 1947L, 2352L, 2698L, 2369L, 1798L, 2422L,
> 2509L, 2234L, 2451L, 2139L, 1957L, 799L, 787L, 701L, 781L, 808L,
> 582L, 770L, 752L, 801L, 865L, 608L, 620L, 677L, 775L, 722L, 1030L,
> 606L, 729L, 1638L, 1408L, 1045L, 1685L, 1109L, 1210L, 1419L,
> 1048L, 1129L, 1549L, 1325L, 1315L, 1838L, 1066L, 1295L, 1499L,
> 1472L, 1139L), lp = c(NA, NA, 46.31, NA, NA, 43.8, NA, NA, 43.91,
> NA, NA, 44.47, NA, NA, 45.16, NA, NA, 43.57, 40.65, NA, NA, 40.04,
> NA, NA, 41.33, NA, NA, 40.75, NA, NA, 42.04, NA, NA, 40.35, NA,
> NA, 43.682, NA, NA, 41.712, NA, NA, 42.566, NA, NA, 43.228, NA,
> NA, 43.63, NA, NA, 42.058, NA, NA, NA, 45.19, NA, NA, 41.91,
> NA, NA, 43.86, NA, NA, 44.48, NA, NA, 44.34, NA, NA, 43.03, NA,
> NA, NA, 44.08, NA, NA, 41.39, NA, NA, 42.48, NA, NA, 44.13, NA,
> NA, 43.39, NA, NA, 42.82, 42.18, NA, NA, 41.42, NA, NA, 41.25,
> NA, NA, 42.31, NA, NA, 43.22, NA, NA, 40.52, NA, NA), lnth = c(NA,
> NA, 1.151, NA, NA, 1.135, NA, NA, 1.109, NA, NA, 1.117, NA, NA,
> 1.107, NA, NA, 1.196, 1.255, NA, NA, 1.229, NA, NA, 1.158, NA,
> NA, 1.214, NA, NA, 1.152, NA, NA, 1.194, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> 1.2, NA, NA, 1.219, NA, NA, 1.115, NA, NA, 1.205, NA, NA, 1.238,
> NA, NA, 1.244, NA, NA, NA, 1.096, NA, NA, 1.021, NA, NA, 1.055,
> NA, NA, 1.058, NA, NA, 1.026, NA, NA, 1.115, 1.202, NA, NA, 1.161,
> NA, NA, 1.168, NA, NA, 1.189, NA, NA, 1.204, NA, NA, 1.277, NA,
> NA)), .Names = c("FIELD", "REP", "PEDIGREE", "yield", "lp", "lnth"
> ), row.names = c(NA, -108L), class = "data.frame")
>
>
>
>
>
>
> R version 3.2.1 (2015-06-18)
>
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> Running under: Windows 7 x64 (build 7601) Service Pack 1
>
>
>
> locale:
>
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
>
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>
> [5] LC_TIME=English_United States.1252
>
>
>
> attached base packages:
>
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
>
>
> other attached packages:
>
> [1] agricolae_1.2-1 asreml_3.0      lattice_0.20-31 ggplot2_1.0.1   dplyr_0.4.2     plyr_1.8.3
>
>
>
> loaded via a namespace (and not attached):
>
>  [1] spdep_0.5-88     Rcpp_0.12.1      cluster_2.0.2    magrittr_1.5     splines_3.2.1    MASS_7.3-41
>
>  [7] munsell_0.4.2    colorspace_1.2-6 R6_2.0.1         stringr_1.0.0    tools_3.2.1      parallel_3.2.1
>
> [13] grid_3.2.1       gtable_0.1.2     nlme_3.1-122     coda_0.17-1      DBI_0.3.1        deldir_0.1-9
>
> [19] lazyeval_0.1.10  assertthat_0.1   digest_0.6.8     Matrix_1.2-1     reshape2_1.4.1   sp_1.2-1
>
> [25] stringi_1.0-1    klaR_0.6-12      LearnBayes_2.15  scales_0.3.0     boot_1.3-17      combinat_0.0-8
>
> [31] proto_0.3-10
>
> Thanks.
> Nilesh
>

Sarah Goslee
http://www.numberwright.com



More information about the R-help mailing list