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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With