Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the highest values for two unrelated columns?

Tags:

r

data.table

Let's say I have a data.table that looks like this:

library(data.table)

dt <-
  rowwiseDT(
    group=, a=, b=,
    "a", 1, 10,
    "a", 10, 1,
    "a", 9, 9,
    "b", 9, 9,
    "b", 1, 1,
    "c", 10, 10
  )

group     a     b
   <char> <num> <num>
1:      a     1    10
2:      a    10     1
3:      a     9     9
4:      b     9     9
5:      b     1     1
6:      c    10    10

How would I summarize this to one row per group while maximizing both a and b (which are not related values)?

Like this:

group     a     b
   <char> <num> <num>
3:      a     9     9
4:      b     9     9
6:      c    10    10

Where a's row is 9 and 9 even though that's not the max value for either column.

like image 542
sjenkins Avatar asked Sep 11 '25 19:09

sjenkins


1 Answers

Find the maximum of sum of both values:

dt[, .SD[which.max(a + b)], by = group]

#>     group     a     b
#>    <char> <num> <num>
#> 1:      a     9     9
#> 2:      b     9     9
#> 3:      c    10    10

or if you have more columns, you can sum all the numeric ones like this:

dt[, .SD[which.max(rowSums(.SD))], by = group, .SDcols = is.numeric]

Created on 2025-09-01 with reprex v2.1.1

like image 63
M-- Avatar answered Sep 13 '25 11:09

M--