How to Group and Summarize Data in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Group and Summarize Data in R appeared first on Data Science Tutorials

How to Group and Summarize Data in R?, Grouping and summarising data are two of the most frequent actions you’ll conduct in data analysis.

How to add labels at the end of each line in ggplot2? (datasciencetut.com)

Fortunately, you can easily organize and summarise data using the R dplyr library.

The dplyr package must initially be loaded before you may use any of its functions:

Let’s install the dplyr package (if not already installed)

install.packages('dplyr')
library(dplyr)

The built-in R dataset mtcars will be used to demonstrate numerous examples of how to group and summarise data using the functions in dplyr.

How to Remove Columns from a data frame in R – Data Science Tutorials

Obtain mtcars dataset in rows and columns.

dim(mtcars)
[1] 32 11

Now we can view the first six rows of mtcars

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

We’ll group and summarise data using the following fundamental syntax.

Arrange Data by Month in R with example – Data Science Tutorials

data %>%
  group_by(col_name) %>%
  summarize(summary_name = summary_function)

Note: The functions summarize() and summarise() are equivalent.

Example 1: Search for Mean & Median by Group

The mean and median, as well as other measures of central tendency by group, can be calculated using the code below.

cylinder #find mean mpg

mtcars %>%
  group_by(cyl) %>%
  summarize(mean_mpg = mean(mpg, na.rm = TRUE))
A tibble: 3 x 2
    cyl mean_mpg
1     4     26.7
2     6     19.7
3     8     15.1

To find median mpg by cylinder

Filtering for Unique Values in R- Using the dplyr Data Science Tutorials

mtcars %>%
  group_by(cyl) %>%
  summarize(median_mpg = median(mpg, na.rm = TRUE))
 A tibble: 3 x 2
    cyl median_mpg
1     4       26 
2     6       19.7
3     8       15.2

Example 2: Find Spread Measures by Group

The standard deviation, interquartile range, and median absolute deviation may all be calculated for each group using the following code.

by cylinder, find sd, IQR, and mad

mtcars %>%
  group_by(cyl) %>%
  summarize(sd_mpg = sd(mpg, na.rm = TRUE),
            iqr_mpg = IQR(mpg, na.rm = TRUE),
            mad_mpg = mad(mpg, na.rm = TRUE))
 A tibble: 3 x 4
    cyl sd_mpg iqr_mpg mad_mpg
1     4   4.51    7.60    6.52
2     6   1.45    2.35    1.93
3     8   2.56    1.85    1.56

Example 3: Discover Count by Group

The R code below demonstrates how to find the count and the unique count by the group.

How to draw heatmap in r: Quick and Easy way – Data Science Tutorials

To find unique row count and row count by the cylinder.

mtcars %>%
  group_by(cyl) %>%
  summarize(count_mpg = n(),
            u_count_mpg = n_distinct(mpg))
 A tibble: 3 x 3
    cyl count_mpg u_count_mpg
1     4        11           9
2     6         7           6
3     8        14          12

Example 4: Calculate Percentile by Group

The 90th percentile of data for mpg by cylinder group can be determined using the following code.

Error in sum(List) : invalid ‘type’ (list) of argument – Data Science Tutorials

For each cylinder group, find the 90th percentile of the mpg.

mtcars %>%
  group_by(cyl) %>%
  summarize(quant90 = quantile(mpg, probs = .9))
cyl quant90
  <dbl>   <dbl>
1     4    32.4
2     6    21.2
3     8    18.3

The post How to Group and Summarize Data in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)