Using R for Grouping statistics

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.