Grouped statistics datasets are a very common requirement, and R also has a corresponding package that supports grouping statistics for datasets. I have tried to write a section R code to complete the group statistics dataset, support the formula, feel easy to use. Code sharing at the end of the article.
How to use:
Step 1:source (' AGGREGATESUMMARY.R ')
Step 2:pastecs_summary (Mpg+hp+wt~am,data=mtcars)
The results of the implementation are as follows:
Source code for AGGREGATESUMMARY.R:
Library (PLYR) library (STRINGR) library (pastecs) Parseformula <-function (Formula = "... ~ variable", Varnames, Value.var = "value") {Remove.placeholder <-function (x) x[x! = "."] Replace.remainder <-function (x) {if (any (x = = "...")) C (x[x! = "..."], remainder) Else x} if (Is.formula (form ULA) {formula <-Str_c (Deparse (formula), collapse = "")} if (Is.character (formula)) {dims <-str_ Split (formula, fixed ("~")) [[1]] formula <-lapply (Str_split (dims, "[+*]"), Str_trim) formula <-lapply (for Mula, Remove.placeholder) all_vars <-unlist (Formula) if (any (All_vars = = "...")) {remainder <-setd IFF (Varnames, C (All_vars, Value.var)) Formula <-lapply (formula, Replace.remainder)}} if (!is.list (Formul A) {stop ("Don t know how to parse", formula, call. = FALSE)} lapply (formula, as.quoted)}evalformula <-function (Formula,data) {Fo<-parseformula (Formula) lapply (fo,eval.quoted,envir=data)}pastecs_summary<-function (formula,data) {Tmplist<-evalformula (formula,data) df1<-as.data.frame (tmplist[1 ]) Uni<-unique (tmplist[[2]][[1]) lst<-list () for (I in uni) {lst[[paste (names (tmplist[[2]]), i)]]<-stat.desc (Df1[which (Tmplist[[2]][[1]]==i),])} Return (LST)}
Using R for Grouping statistics