Skip to content

Data Science Tutorials

For Data Science Learners

  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • Confidence Intervals in R
    Confidence Intervals in R R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
How to Group and Summarize Data in R

How to Group and Summarize Data in R

Posted on June 27June 26 By Admin No Comments on How to Group and Summarize Data in R

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

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Tips for Rearranging Columns in R
Next Post: Find the Maximum Value by Group in R

Related Posts

  • Group By Minimum in R
    Group By Minimum in R R
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  •  Identify positions in R R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • Solving Systems of Equations in R R
  • Making games in R- Nara and eventloop Game Changers
    Making games in R- Nara and eventloop Game Changers Machine Learning

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Mastering the map() Function in R R
  • Descriptive statistics in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • Determine the significance of a mediation effect in R
    Determine the significance of a mediation effect in R R
  • Descriptive Statistics in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R
  • How to deal with text in R
    How to deal with text in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme