- 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 absolute maximum of each group in data.table object in R?
To find the absolute maximum of each group in data.table object in R, we can follow the below steps −
First of all, create a data.table object.
Then, use summarise_each function of dplyr package along with which.max and abs function to find the absolute maximum of each group after grouping with group_by.
Example 1
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) Class<-sample(c("I","II","III"),25,replace=TRUE) Response<-sample(-50:50,25) DT1<-data.table(Class,Response) DT1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Class Response 1: II 25 2: I -29 3: II 14 4: III 35 5: III -3 6: I -10 7: III -22 8: III -28 9: II -15 10: I -41 11: I -8 12: III -1 13: II -11 14: II 9 15: III 45 16: II -23 17: I 42 18: II -16 19: III 44 20: II -47 21: III 37 22: III -27 23: I -40 24: I 18 25: II 3 Class Response
Find the absolute maximum of each group
Using summarise_each function of dplyr package along with which.max and abs function to find the absolute maximum of each group for column Response after grouping with group_by as shown below −
library(data.table) Class<-sample(c("I","II","III"),25,replace=TRUE) Response<-sample(-50:50,25) DT1<-data.table(Class,Response) library(dplyr) DT1 %>% group_by(Class) %>% summarise_each(funs(.[which.max(abs(.))]))
Output
# A tibble: 3 x 2 Class Response <chr> <int> 1 I 42 2 II -47 3 III 45
Example 2
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) Factor<-sample(c("F1","F2","F3","F4","F5"),25,replace=TRUE) DV<-rnorm(25) DT2<-data.table(Factor,DV) DT2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Factor DV 1: F5 -2.17198942 2: F2 1.86828270 3: F5 -0.13921697 4: F1 -1.12527133 5: F2 0.30994193 6: F3 -0.85019956 7: F1 -0.53960590 8: F4 0.71515012 9: F1 0.95894780 10: F1 0.69673391 11: F1 1.59025968 12: F1 0.54539870 13: F2 1.03472636 14: F2 -0.86223774 15: F5 1.09875408 16: F5 0.76012240 17: F2 -0.81212071 18: F4 0.16992534 19: F4 0.15300303 20: F3 -1.16854925 21: F5 0.83239589 22: F5 -0.43753269 23: F4 0.03010316 24: F3 0.15060870 25: F5 -0.84028548 Factor DV
Find the absolute maximum of each group
Using summarise_each function of dplyr package along with which.max and abs function to find the absolute maximum of each group for column DV after grouping with group_by as shown below −
library(data.table) Factor<-sample(c("F1","F2","F3","F4","F5"),25,replace=TRUE) DV<-rnorm(25) DT2<-data.table(Factor,DV) library(dplyr) DT2 %>% group_by(Factor) %>% summarise_each(funs(.[which.max(abs(.))]))
Output
# A tibble: 5 x 2 Factor DV <chr> <dbl> 1 F1 1.59 2 F2 1.87 3 F3 -1.17 4 F4 0.715 5 F5 -2.17
- Related Articles
- How to find the absolute maximum of each group in R data frame?
- How to find the maximum of each row in an R data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- How to change the first value for each group in data.table object in R?
- Find the number of non-missing values in each group of an R data frame.
- How to find the difference of values of each row from previous by group in an R data frame?
- How to find the absolute distance between columns of an R data frame?
- How to find the group-wise median in an R data.table object?
- How to find the maximum value in an R data frame?
- How to find the position of maximum of each numerical column if some columns are categorical in R data frame?
- How to find the absolute maximum of a matrix with sign if it contains negative values in R?
- How to find the maximum value for each column of a matrix in R?
- How to find the frequency of each value in an R data frame?
- Find the number of non-missing values in each column by group in an R data frame.\n
- How to find the maximum of each outcome of two throws of a die in R?
