Open In App

Get max value of column 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 get the maximum value of a column by group using the R Programming Language.

How to get the maximum value of the column by group

R language offers various methods to get the maximum value of the column by the group. By using these methods provided by R, it is possible to get the maximum value of the column. Some of the methods to get the maximum value of the column are:

Get the max value of the column by group using Base R

This method is used to get the maximum value of the column by group using Base R. In this example, we created a data frame and found the maximum value of a column.

R
name <- c('a','a','b','b','b','c','c','d')
age <- c(1:8)

# Creating dataframe
df <- data.frame(name, age)
print(df)

# Group by 'name' and find the maximum value of 'age' for each group
print("The maximum value of column by group is")
res <- aggregate(age ~ name, data = df, FUN = max)
print(res)

Output:

  name age
1 a 1
2 a 2
3 b 3
4 b 4
5 b 5
6 c 6
7 c 7
8 d 8

[1] "The maximum value of column by group is"
name age
1 a 2
2 b 5
3 c 7
4 d 8

In this example, we created a data frame and finded the maximum value of a column.

R
student=c('m','n','n','o','o','m')
    id=c(601:606)

#creating dataframe
df = data.frame(student, id)                          
print(df)

print("The maximum value of column by group is")
res<- aggregate(id ~ student,       
                       data = df,
                       FUN = max)
print(res)

Output:

  student  id
1 m 601
2 n 602
3 n 603
4 o 604
5 o 605
6 m 606

[1] "The maximum value of column by group is"
student id
1 m 606
2 n 603
3 o 605

Get maximum value of column by using the package ‘dplyr

This method is used to get maximum value of column by group using the package dplyr. In this example, we created a data frame and finded the maximum value of a column.

R
# Load the dplyr package
library(dplyr)

# Sample data frame
df <- data.frame(
  group = c("A", "A", "B", "B", "C", "C"),
  value = c(10, 20, 15, 25, 5, 10)
)
print(df)

# Finding maximum value 
max_values <- df %>%
  group_by(group) %>%
  summarise(max_value = max(value))

print(max_values)

Output:

  group value
1 A 10
2 A 20
3 B 15
4 B 25
5 C 5
6 C 10

A tibble: 3 × 2
group max_value
<chr> <dbl>
1 A 20
2 B 25
3 C 10

In this example, we created a data frame and finded the maximum value of a column.

R
# Load the dplyr package
library(dplyr)

# Sample data frame
df <- data.frame(
  student = c('m','n','o','q','q','m','s','s'),
  id = c(5:12)
)
print(df)

# Finding maximum value 
max_values <- df %>%
  group_by(student) %>%
  summarise(max_value = max(id))

print(max_values)

Output:

  student id
1 m 5
2 n 6
3 o 7
4 q 8
5 q 9
6 m 10
7 s 11
8 s 12

A tibble: 5 × 2
student max_value
<chr> <int>
1 m 10
2 n 6
3 o 7
4 q 9
5 s 12

Conclusion

In conclusion, we learned about how to get maximum value of column by group using various methods. R provides versatile tools to get maximum value of column by group efficiently.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads