Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution 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
  • The Multinomial Distribution in R
    The Multinomial Distribution in R R
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
Count Observations by Group in R

Count Observations by Group in R

Posted on June 6June 4 By Jim No Comments on Count Observations by Group in R
Tweet
Share
Share
Pin

Count Observations by Group in R, want to count the number of observations by the group.

Fortunately, the count() function from the dplyr library makes this simple.

Using the data frame below, this tutorial shows numerous examples of how to utilize this function in practice.

Change ggplot2 Theme Color in R- Data Science Tutorials

Count Observations by Group in R

Let’s create a data frame

df <- data.frame(Q1 = c('A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
                 Q2 = c('G', 'G', 'F', 'G', 'F', 'F', 'F', 'G', 'G', 'F', 'F', 'F'),
                 Q3 = c(4, 13, 7, 8, 15, 15, 17, 9, 21, 22, 25, 31))
df
    Q1 Q2 Q3
1   A  G  4
2   A  G 13
3   A  F  7
4   B  G  8
5   B  F 15
6   B  F 15
7   B  F 17
8   B  G  9
9   C  G 21
10  C  F 22
11  C  F 25
12  C  F 31

Approach 1: Count by One Variable

The code below demonstrates how to count the total number of players in each team(Q1).

How to compare variances in R – Data Science Tutorials

total observations by the ‘Q1’ variable

library(dplyr)
df %>% count(Q1)
   Q1 n
1  A 3
2  B 5
3  C 4

We can observe from the output that:

There are three players on Team A.

Team B consists of five players.

There are four players on Team C.

This single count() function gives us a good indication of how many players are in each squad.

It’s worth noting that we can sort the counts if we want to.

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

count total observations by the ‘Q1’ variable

df %>% count(Q1, sort=TRUE)
   Q1 n
1  B 5
2  C 4
3  A 3

Approach 2: Count by Multiple Variables

We can sort by many variables as well.

‘Q1’ and ‘Q3’ are used to count the total number of observations.

df %>% count(Q1, Q3)
   Q1 Q3 n
1   A  4 1
2   A  7 1
3   A 13 1
4   B  8 1
5   B  9 1
6   B 15 2
7   B 17 1
8   C 21 1
9   C 22 1
10  C 25 1
11  C 31 1

Approach 3: Weighted Count

Another variable can be used to “weight” the numbers of one variable. The following code, for example, demonstrates how to tally the total observations per team using the variable ‘Q3’ as the weight.

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

df %>% count(Q1, wt=Q3)
   Q1  n
1  A 24
2  B 64
3  C 99

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to add columns to a data frame in R
Next Post: How to Count Distinct Values in R

Related Posts

  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • How to perform TBATS Model in R
    How to perform TBATS Model in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R 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
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R
  • How to Filter Rows In R
    How to Filter Rows In R? R
  • What is the best way to filter by row number in R?
    What is the best way to filter by row number in R? R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • Bind together two data frames by their rows or columns in R
    Bind together two data frames by their rows or columns in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme