- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the maximum using aggregate and get the output with all the columns in R?
When we use aggregate function to find maximum or any other value, the output of the aggregation does not provide all the columns that corresponds to the maximum value. Therefore, we need to merge the data frame obtained by using aggregate with the original data frame. In this way, we will get only those rows that are common between the new data frame and the original one.
Example
Consider the below data frame −
set.seed(99) x1<-rep(c(1,2,3,4,5),times=4) x2<-sample(1:100,20) x3<-rep(1:10,2) x4<-rep(c("C1","C2","C3","C4"),each=5) x5<-sample(1:10,20,replace=TRUE) x6<-sample(1:1000,20,replace=TRUE) df<-data.frame(x1,x2,x3,x4,x5,x6) df
Output
x1 x2 x3 x4 x5 x6 1 1 48 1 C1 1 123 2 2 33 2 C1 7 285 3 3 44 3 C1 2 301 4 4 22 4 C1 7 268 5 5 62 5 C1 6 713 6 1 32 6 C2 5 867 7 2 13 7 C2 8 509 8 3 20 8 C2 7 616 9 4 31 9 C2 10 308 10 5 68 10 C2 6 755 11 1 9 1 C3 5 806 12 2 82 2 C3 10 315 13 3 88 3 C3 1 552 14 4 30 4 C3 6 103 15 5 86 5 C3 6 419 16 1 84 6 C4 7 395 17 2 95 7 C4 4 422 18 3 14 8 C4 5 818 19 4 4 9 C4 7 68 20 5 78 10 C4 1 879
Aggregation examples for maximum value and getting the all column names −
merge(aggregate(x2~x4,df,FUN=max),df) x4 x2 x1 x3 x5 x6 1 C1 62 5 5 6 713 2 C2 68 5 10 6 755 3 C3 88 3 3 1 552 4 C4 95 2 7 4 422 merge(aggregate(x2~x1,df,FUN=max),df) x1 x2 x3 x4 x5 x6 1 1 84 6 C4 7 395 2 2 95 7 C4 4 422 3 3 88 3 C3 1 552 4 4 31 9 C2 10 308 5 5 86 5 C3 6 419 merge(aggregate(x2~x3,df,FUN=max),df) x3 x2 x1 x4 x5 x6 1 1 48 1 C1 1 123 2 10 78 5 C4 1 879 3 2 82 2 C3 10 315 4 3 88 3 C3 1 552 5 4 30 4 C3 6 103 6 5 86 5 C3 6 419 7 6 84 1 C4 7 395 8 7 95 2 C4 4 422 9 8 20 3 C2 7 616 10 9 31 4 C2 10 308 merge(aggregate(x5~x4,df,FUN=max),df) x4 x5 x1 x2 x3 x6 1 C1 7 4 22 4 268 2 C1 7 2 33 2 285 3 C2 10 4 31 9 308 4 C3 10 2 82 2 315 5 C4 7 4 4 9 68 6 C4 7 1 84 6 395 merge(aggregate(x5~x1,df,FUN=max),df) x1 x5 x2 x3 x4 x6 1 1 7 84 6 C4 395 2 2 10 82 2 C3 315 3 3 7 20 8 C2 616 4 4 10 31 9 C2 308 5 5 6 62 5 C1 713 6 5 6 68 10 C2 755 7 5 6 86 5 C3 419 merge(aggregate(x6~x4,df,FUN=max),df) x4 x6 x1 x2 x3 x5 1 C1 713 5 62 5 6 2 C2 867 1 32 6 5 3 C3 806 1 9 1 5 4 C4 879 5 78 10 1
- Related Articles
- How to find the maximum of factor levels in numerical column and return the output including other columns in the R data frame?
- How to multiply large numbers with all digits in the output in R?
- How to find the minimum and maximum of columns values in an R data frame?
- Get the maximum value of a column with MySQL Aggregate function
- How to find the mean of all columns by group in R?
- Java Program to display table with columns in the output using Formatter
- How to aggregate matrix columns by row names in R?
- How to find the median of all columns in an R data frame?
- Select aggregate function and all other columns in MySQL
- How to find the column maximum if some columns are categorical in R data frame?
- How to find the frequency for all columns based on a condition in R?
- How to get the maximum number of occupied rows and columns in a worksheet in Selenium with python?
- How to find the maximum value of all matrices stored in an R list?
- How to get the summary statistics including all basic statistical values for R data frame columns?
- How to subset R data frame rows and keep the rows with NA in the output?

Advertisements