# 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  C1  X A  42  X A 133  X A  74  X B  85  X B 156  Y B 157  Y A 178  Y A  99  Y C 2110 Z C 2211 Z C 2512 Z C 31[1] "counting number of observations in column A"  A n1 X 52 Y 43 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  C1  X A  42  X A 133  X A  74  X B  85  X C 156  Y B 157  Y C 178  Z C 259  Z C 31[1] "counting number of observations in column"  B n1 C 42 A 33 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  C1  X A  42  X A 133  X A  74  X B  85  X B 156  Y B 157  Y A 178  Y A  99  Y C 2110 Z C 2211 Z C 2512 Z C 31[1]"count by multiple variables"  A B n1 X A 32 X B 23 Y A 24 Y B 15 Y C 16 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 Marks1  21 Female    782  21   Male    923  19   Male    824  22 Female    755  20   Male    806  19 Female    827  23   Male    858  20 Female    909  21   Male    92[1] "count by multiple variables"  Age Marks n1  19    82 22  20    80 13  20    90 14  21    78 15  21    92 26  22    75 17  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  C1  X A  42  X A 133  X A  74  X B  85  X B 156  Y B 157  Y A 178  Y A  99  Y C 2110 Z C 2211 Z C 2512 Z C 31[1] "counting weight of variables in a column"  A  n1 X 472 Y 623 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   n1  19 1692  20 1703  21 2584  22  755  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.

Article Tags :