Open In App

Count Observations by Group in R

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to count observations by Group in the R Programming Language.

How to Count Observations by Group in R

R language offers many built-in functions to count the number of observations in the datasets such as matrices, data frames, lists, arrays, and vectors. To count the observations by Group, we can use the count() function from the dplyr library. This is a convenient way to count the number of observations uniquely.

Syntax:

library(dplyr)
df %>%
count( )

Count Observations by One Variable

In this example, we are counting a single variable of data entries from the dataframe.

R
#creating data frame
df1 <- data.frame(A= c('X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z'),
                 B= c('A', 'A', 'A', 'B', 'B', 'B', 'A', 'A', 'C', 'C', 'C', 'C'),
                C= c(4, 13, 7, 8, 15, 15, 17, 9, 21, 22, 25, 31))

#printing the data frame
print(df1)
library(dplyr)

print("counting number of observations in column A")
df1 %>% count(A)

Output:

   A B  C
1 X A 4
2 X A 13
3 X A 7
4 X B 8
5 X B 15
6 Y B 15
7 Y A 17
8 Y A 9
9 Y C 21
10 Z C 22
11 Z C 25
12 Z C 31

[1] "counting number of observations in column A"
A n
1 X 5
2 Y 4
3 Z 3

In this example, we are counting a single variable of data entries from the dataframe.

R
#creating data frame
df1 <- data.frame(A= c('X', 'X', 'X', 'X', 'X', 'Y', 'Y',' Z', 'Z'),
                 B= c('A', 'A', 'A', 'B', 'C', 'B', 'C', 'C', 'C'),
                C= c(4, 13, 7, 8, 15, 15, 17, 25, 31))

#printing the data frame
print(df1)
library(dplyr)

print("counting number of observations in column")
df1 %>% count(B,sort=TRUE)

Output:

   A B  C
1 X A 4
2 X A 13
3 X A 7
4 X B 8
5 X C 15
6 Y B 15
7 Y C 17
8 Z C 25
9 Z C 31

[1] "counting number of observations in column"
B n
1 C 4
2 A 3
3 B 2

Count Observations by Multiple Variables

In this example, we are counting multiple variables of data entries from the dataframe.

R
#creating data frame
df1 <- data.frame(A= c('X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z'),
                 B= c('A', 'A', 'A', 'B', 'B', 'B', 'A', 'A', 'C', 'C', 'C', 'C'),
                C= c(4, 13, 7, 8, 15, 15, 17, 9, 21, 22, 25, 31))

#printing the data frame
print(df1)
library(dplyr)

print("count by multiple variables")
df1 %>% count(A,B)

Output:

   A B  C
1 X A 4
2 X A 13
3 X A 7
4 X B 8
5 X B 15
6 Y B 15
7 Y A 17
8 Y A 9
9 Y C 21
10 Z C 22
11 Z C 25
12 Z C 31

[1]"count by multiple variables"
A B n
1 X A 3
2 X B 2
3 Y A 2
4 Y B 1
5 Y C 1
6 Z C 3

In this example, we are counting multiple variables of data entries from the dataframe.

R
library(dplyr)

df <- data.frame(
  Age = c(21, 21, 19, 22,20,19, 23, 20, 21),
  Gender = c("Female", "Male", "Male", "Female", "Male", "Female", "Male", "Female", 
                         "Male"),
  Marks = c( 78, 92,82,75,80, 82,85, 90, 92)
)

print(df)
print("count by multiple variables")
df %>% count(Age,Marks)

Output:

  Age Gender Marks
1 21 Female 78
2 21 Male 92
3 19 Male 82
4 22 Female 75
5 20 Male 80
6 19 Female 82
7 23 Male 85
8 20 Female 90
9 21 Male 92

[1] "count by multiple variables"
Age Marks n
1 19 82 2
2 20 80 1
3 20 90 1
4 21 78 1
5 21 92 2
6 22 75 1
7 23 85 1

Count Observations by Weighted Count

These approach counts of one variable by another variable. In the below example, we are counting the weights of every variable according to its data points.

R
#creating data frame
df1 <- data.frame(A= c('X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z'),
                 B= c('A', 'A', 'A', 'B', 'B', 'B', 'A', 'A', 'C', 'C', 'C', 'C'),
                C= c(4, 13, 7, 8, 15, 15, 17, 9, 21, 22, 25, 31))

#printing the data frame
print(df1)
library(dplyr)

print("counting weight of variables in a column")
df1 %>% count(A, wt=C)

Output:

   A B  C
1 X A 4
2 X A 13
3 X A 7
4 X B 8
5 X B 15
6 Y B 15
7 Y A 17
8 Y A 9
9 Y C 21
10 Z C 22
11 Z C 25
12 Z C 31

[1] "counting weight of variables in a column"
A n
1 X 47
2 Y 62
3 Z 78

In the below example, we are counting the weights of every variable according to its data points.

R
library(dplyr)

df <- data.frame(
  Age = c(21, 21, 19, 22,20,19, 23, 20, 21),
  Gender = c("Female", "Male", "Male", "Female","Male","Female","Male","Female","Male"),
  Marks = c( 78, 92,82,75,80, 87,85, 90, 88)
)

print("weighted count of observations")
df %>% count(Age, wt = Marks)

Output:

[1] "weighted count of observations"
Age n
1 19 169
2 20 170
3 21 258
4 22 75
5 23 85

Conclusion

In Conclusion, we learned how to count observations by Group in a data frame using various approaches. R language offers versatile tools to count observations by Group in R.



Similar Reads

Count Unique Values by Group in R
In the article, we are going to discuss how to count the number of unique values by the group in R Programming Language. So let's take the following example, Suppose you have a dataset with multiple columns like this: class age age_group 1 A 20 YOUNG 2 B 15 KID 3 C 45 OLD 4 B 14 KID 5 A 21 YOUNG 6 A 22 YOUNG 7 C 47 OLD 8 A 19 YOUNG 9 B 16 KID 10 C
2 min read
Count number of rows within each group in R DataFrame
DataFrame in R Programming Language may contain columns where not all values are unique. The duplicate values in the dataframe can be sectioned together into one group. The frequencies corresponding to the same columns' sequence can be captured using various external packages in R programming language. Method 1 : Using dplyr package The "dplyr" pac
5 min read
Count non-NA values by group in DataFrame in R
In this article, we will discuss how to count non-NA values by the group in dataframe in R Programming Language. Method 1 : Using group_by() and summarise() methods The dplyr package is used to perform simulations in the data by performing manipulations and transformations. The group_by() method in R programming language is used to group the specif
5 min read
Group by one or more variables using Dplyr in R
The group_by() method is used to divide and segregate date based on groups contained within the specific columns. The required column to group by is specified as an argument of this function. It may contain multiple column names. Syntax: group_by(col1, col2, ...) Example 1: Group by one variable C/C++ Code # installing required libraries library(
2 min read
How to Create Frequency Table by Group using Dplyr in R
In this article, we will be looking at the approach to creating a frequency table group with its working examples in the R programming language. Create Frequency Table by Group using dplyr package: In this approach to create the frequency table by group, the user first needs to import and install the dplyr package in the working console, and then t
2 min read
Rank variable by group using Dplyr package in R
In this article, we are going to see how to rank the variable by group using dplyr in R Programming Language. The dplyr package in R is used to perform mutations and data manipulations in R. It is particularly useful for working with data frames and data tables. The package can be downloaded and installed into the working directory using the follow
2 min read
Group by function in R using Dplyr
Group_by() function belongs to the dplyr package in the R programming language, which groups the data frames. Group_by() function alone will not give any output. It should be followed by summarise() function with an appropriate action to perform. It works similar to GROUP BY in SQL and pivot table in excel. Syntax: group_by(col,...) Syntax: group_b
2 min read
How to select row with maximum value in each group in R Language?
In R Programming Language, to select the row with the maximum value in each group from a data frame, we can use various approaches as discussed below. Consider the following dataset with multiple observations in sub-column. This dataset contains three columns as sr_no, sub, and marks. Creating Dataset : Here we are creating dataframe for demonstrat
4 min read
How to sum a variable by group in R?
In this article, let's discusses how to find sum of a variable by the group in R Programming Language. Dataset in Use: [caption width="800"]Table-1[/caption] Expected output: [caption width="800"]Group by Category[/caption]ApproachCreate dataframeSet values to required parametersPass to the function in useDisplay dataframeMethod 1: Using aggregate
2 min read
How to find group-wise summary statistics for R dataframe?
Finding group-wise summary statistics for the dataframe is very useful in understanding our data frame. The summary includes statistical data: mean, median, min, max, and quartiles of the given dataframe. The summary can be computed on a single column or variable, or the entire dataframe. In this article, we are going to see how to find group-wise
4 min read
Article Tags :