Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • OLS Regression in R
    OLS Regression in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • Ogive Graph in R
    Ogive Graph in R R
How to Group and Summarize Data in R

How to Group and Summarize Data in R

Posted on June 27June 26 By Jim No Comments on How to Group and Summarize Data in R
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • ggdogs on ggplot2
    ggdogs on ggplot2 R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • How to move from Junior Data Scientist
    How to move from Junior Data Scientist Machine Learning
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Correlation Coefficient p value in R
    Correlation Coefficient p value in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme