Aggregating basic statistics group-wise in R

November 10, 2016

(This article was first published on Krishna's R Blog, and kindly contributed to R-bloggers)

Many times, while doing Statistical analysis, we have to evaluate the descriptive statistics like mean, standard deviation and so on for a number of variables, group-wise. Most of the Statistical packages like SAS, SPSS and so on provide these features. In R, the data.table package is very useful for aggregating these types of results and to tabulate them. It offers fast aggregation of large data , fast ordered joins, fast add/modify/delete of columns by group using no copies at all, list columns and a fast file reader (fread). In addition, the tables package and psych package’s describeBy method were also found to be useful for generating this type of results. As an exercise, the iris data is considered, which contains the data of four variables Sepal Length(SL), Sepal Width(SW), Petal Length(PL) and Petal Width(PW) of three species setosa, versicolor and virginica. Three types of results were generated, utilizing all the three packages listed above.

1.Mean and Standard deviation for all the four variables specie-wise using data-table package

2.Mean and Standard deviation for all the four variables specie-wise using tables package

3.Mean and Standard deviation for all the four variables specie-wise using psych package


To leave a comment for the author, please follow the link and comment on their blog: Krishna's R Blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)