[R] help with "by" function

DIGHE, NILESH [AG/2362] nilesh.dighe at monsanto.com
Tue May 12 20:03:17 CEST 2015


Hi,
I have an anonymous function called function(x) that will run anova, run HSD.test on the model, and then sort the results.  I am passing this anonymous function to the "by" function to get results by "Isopair" factor which is my index variable.  Since I want to run the anova on multiple dependent variables including "Trait1" & "Trait2", I am calling the dependent variable, "trait_names" in the model and then passing the "by" function to another function called "funC" which takes the "trait_names" as an argument to execute.

After I execute the funC(data_set$trait1), I am getting the results by Isopair but the results for the two Isopairs (Isopair-A &Isopair-B) are the same which I know is not correct.  It looks like the data is not getting split by Isopairs and so ALL data is used in anova for both Isopairs.  Any help in modifying function, funC or any other ways to achieve the desired outcome will be highly appreciated.

Thanks.  Nilesh

R code, data set, and session info is pasted below.
R code:
library(agricolae)
funC<- function(trait_names){
  by(data_set, data_set$Isopair,function(x){
  mod<- aov(trait_names~ STGgroup*Field + Rep%in%Field, data=data_set)
  out<-HSD.test(mod,"STGgroup",group=TRUE,console=TRUE)
  dfout<- arrange(data.frame(out$groups),desc(trt))
  })
}
Results:
##execute funC function for Trait1 & Trait2
funC(data_set$Trait1)


data_set$Isopair: Isopair-A

      trt    means M

1 STG     776.9167 a

2 Non-STG 779.0833 a

---------------------------------------------------------------------------

data_set$Isopair: Isopair-B

      trt    means M

1 STG     776.9167 a

2 Non-STG 779.0833 a

Data:
data_set<- structure(list(Field = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("LML6", "TZL2"), class = "factor"), Isopair = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Isopair-A", "Isopair-B"
), class = "factor"), STGgroup = structure(c(1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L), .Label = c("Non-STG", "STG"), class = "factor"),
    Rep = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Rep1",
    "Rep2", "Rep3"), class = "factor"), Trait1 = c(686L, 641L,
    642L, 727L, 619L, 562L, 808L, 739L, 744L, 873L, 797L, 868L,
    782L, 783L, 675L, 713L, 762L, 641L, 1009L, 995L, 845L, 1186L,
    912L, 663L), Trait2 = c(45L, 65L, 70L, 35L, 20L, 80L, 70L,
    65L, 70L, 20L, 30L, 35L, 40L, 55L, 35L, 40L, 35L, 40L, 40L,
    35L, 25L, 40L, 35L, 25L)), .Names = c("Field", "Isopair",
"STGgroup", "Rep", "Trait1", "Trait2"), class = "data.frame", row.names = c(NA,
-24L))

Session info:

R version 3.1.3 (2015-03-09)

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] grid      stats     graphics  grDevices utils     datasets  methods   base



other attached packages:

 [1] gridExtra_0.9.1 Hmisc_3.16-0    Formula_1.2-1   survival_2.38-1 caret_6.0-41    ggplot2_1.0.1

 [7] lattice_0.20-30 MASS_7.3-39     dplyr_0.4.1     agricolae_1.2-1



loaded via a namespace (and not attached):

 [1] acepack_1.3-3.3     assertthat_0.1      boot_1.3-15         BradleyTerry2_1.0-6

 [5] brglm_0.5-9         car_2.0-25          cluster_2.0.1       coda_0.17-1

 [9] codetools_0.2-10    colorspace_1.2-6    combinat_0.0-8      DBI_0.3.1

[13] deldir_0.1-9        digest_0.6.8        foreach_1.4.2       foreign_0.8-63

[17] gtable_0.1.2        gtools_3.4.1        iterators_1.0.7     klaR_0.6-12

[21] latticeExtra_0.6-26 lazyeval_0.1.10     LearnBayes_2.15     lme4_1.1-7

[25] magrittr_1.5        Matrix_1.1-5        mgcv_1.8-4          minqa_1.2.4

[29] munsell_0.4.2       nlme_3.1-120        nloptr_1.0.4        nnet_7.3-9

[33] parallel_3.1.3      pbkrtest_0.4-2      plyr_1.8.1          proto_0.3-10

[37] quantreg_5.11       RColorBrewer_1.1-2  Rcpp_0.11.6         reshape2_1.4.1

[41] rpart_4.1-9         scales_0.2.4        sp_1.0-17           SparseM_1.6

[45] spdep_0.5-88        splines_3.1.3       stringr_0.6.2       tools_3.1.3





This e-mail message may contain privileged and/or confidential information, and is intended to be received only by persons entitled
to receive such information. If you have received this e-mail in error, please notify the sender immediately. Please delete it and
all attachments from any servers, hard drives or any other media. Other use of this e-mail by you is strictly prohibited.

All e-mails and attachments sent and received are subject to monitoring, reading and archival by Monsanto, including its
subsidiaries. The recipient of this e-mail is solely responsible for checking for the presence of "Viruses" or other "Malware".
Monsanto, along with its subsidiaries, accepts no liability for any damage caused by any such code transmitted by or accompanying
this e-mail or any attachment.


The information contained in this email may be subject to the export control laws and regulations of the United States, potentially
including but not limited to the Export Administration Regulations (EAR) and sanctions regulations issued by the U.S. Department of
Treasury, Office of Foreign Asset Controls (OFAC).  As a recipient of this information you are obligated to comply with all
applicable U.S. export laws and regulations.

	[[alternative HTML version deleted]]



More information about the R-help mailing list