Find 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
Updated on: 2021-11-09T06:34:20+05:30

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements