Count Observations by Group in R
Last Updated :
26 Mar, 2024
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.
Share your thoughts in the comments
Please Login to comment...