# Get max value of column by group in R

Last Updated : 26 Mar, 2024

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 age1    a   12    a   23    b   34    b   45    b   56    c   67    c   78    d   8[1] "The maximum value of column by group is"  name age1    a   22    b   53    c   74    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  id1       m 6012       n 6023       n 6034       o 6045       o 6056       m 606[1] "The maximum value of column by group is"  student  id1       m 6062       n 6033       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 value1     A    102     A    203     B    154     B    255     C     56     C    10A tibble: 3 Ã— 2  group max_value  <chr>     <dbl>1 A            202 B            253 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 id1       m  52       n  63       o  74       q  85       q  96       m 107       s 118       s 12A tibble: 5 Ã— 2  student max_value  <chr>       <int>1 m              102 n               63 o               74 q               95 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.