Count Number of Cases within Each Group of Data Frame in R (Example) | Counting Rows
In this R tutorial you’ll learn how to count the number of rows within data frame groups.
The content looks as follows:
- Construction of Example Data
- Example 1: Count Cases of One Group in R
- Example 2: Count Cases of Each Group in R
- Video & Further Resources
Let’s dive into it.
Construction of Example Data
In the examples of this R programming tutorial, we’ll use the following data frame as basement:
set.seed(2131415) # Create example data data <- data.frame(x = round(rnorm(100, 0, 3)), group1 = sample(LETTERS[1:2], 100, replace = TRUE), group2 = sample(LETTERS[3:5], 100, replace = TRUE)) head(data) # Print example data # x group1 group2 # 1 2 A C # 2 0 B C # 3 -2 A D # 4 4 B E # 5 0 A E # 6 -5 A D
The previous output of the RStudio console shows the first six rows of our data matrix. Our example data table contains three columns (i.e. the numeric variable x and two grouping columns) and 100 rows. The grouping column group1 contains the values A and B and the grouping column group2 consists of the group indicators C, D, and E.
Now, let’s count the amount of rows of the group combinations of the variables group1 and group2!
Example 1: Count Cases of One Group in R
Example 1 shows how to count the number of rows of one specific group combination, i.e. cases equal to the value A in group1 and equal to C in group2.
For this task, we can use the nrow function and square brackets to subset our data by logical conditions according to our desired groups:
nrow(data[data$group1 == "A" & data$group2 == "C", ]) # Count rows of one group # 16
The previous R code returns the value 16, i.e. 16 row elements of our data frame are equal to the grouping indicators A and C.
Example 2: Count Cases of Each Group in R
Example 2 show a more general approach for counting cases within groups. More precisely, the following R syntax returns the amount of cases within each group combination of our data frame using the aggregate and length functions:
aggregate(x ~ group1 + group2, # Count rows of all groups data = data, FUN = length) # group1 group2 x # 1 A C 16 # 2 B C 21 # 3 A D 13 # 4 B D 20 # 5 A E 12 # 6 B E 18
As you can see based on the previous output of the RStudio console, the previous R programming code illustrates the size of each group in our data frame.
Video & Further Resources
If you need further info on the R programming codes of this post, you might want to watch the following video of my YouTube channel. In the video, I show the content of this tutorial:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might have a look at some of the other tutorials that I have published on Statistics Globe. I have published several articles already.
- aggregate Function in R
- Numbering Rows within Groups of Data Frame
- The nrow Function in R
- The length Function in R
- Subset Data Frame Rows by Logical Condition
- The R Programming Language
Summary: On this page, I illustrated how to aggregate observations in data frames in R. In case you have further comments or questions, let me know in the comments section below.
2 Comments. Leave new
Leave a Reply Cancel reply
Statistics Globe Newsletter
Hi! I have been a frequent traveler to your site while trying to learn the ways of R. I had a challenge with row counts on multiple variables, and your solution was so elegant and easy. Thank you!
Thank you very much for the wonderful feedback! Glad you find my tutorials helpful!