For a data.frame with numeric values, add a new column specifying the column name of the first max/min value each row.
col_max(.data, ..., .name = "max_col")
col_min(.data, ..., .name = "min_col")
A data.table
https://stackoverflow.com/questions/17735859/for-each-row-return-the-column-name-of-the-largest-value
set.seed(199057)
DT <- data.table(matrix(sample(10, 100, TRUE), ncol=10))
DT
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
col_max(DT)
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
#> 1 variable(s) not shown: [max_col <char>]
col_max(DT,V1:V3)
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
#> 1 variable(s) not shown: [max_col <char>]
col_max(DT,.name = "max_col_name")
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
#> 1 variable(s) not shown: [max_col_name <char>]
col_min(DT)
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
#> 1 variable(s) not shown: [min_col <char>]
col_min(DT,2:4)
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1: 8 1 2 2 5 5 9 2 9 4
#> 2: 6 2 1 10 5 7 1 10 9 10
#> 3: 3 1 8 6 10 10 9 2 4 2
#> 4: 1 2 9 1 10 10 2 2 7 7
#> 5: 9 5 4 4 5 10 4 5 8 8
#> 6: 8 10 3 3 8 8 5 1 4 1
#> 7: 10 3 3 7 7 7 8 1 5 5
#> 8: 10 8 10 1 5 10 8 6 3 4
#> 9: 1 7 6 2 6 1 10 6 4 5
#> 10: 5 10 3 3 5 9 2 9 7 7
#> 1 variable(s) not shown: [min_col <char>]
col_max(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species max_col
#> <num> <num> <num> <num> <fctr> <char>
#> 1: 5.1 3.5 1.4 0.2 setosa Sepal.Length
#> 2: 4.9 3.0 1.4 0.2 setosa Sepal.Length
#> 3: 4.7 3.2 1.3 0.2 setosa Sepal.Length
#> 4: 4.6 3.1 1.5 0.2 setosa Sepal.Length
#> 5: 5.0 3.6 1.4 0.2 setosa Sepal.Length
#> ---
#> 146: 6.7 3.0 5.2 2.3 virginica Sepal.Length
#> 147: 6.3 2.5 5.0 1.9 virginica Sepal.Length
#> 148: 6.5 3.0 5.2 2.0 virginica Sepal.Length
#> 149: 6.2 3.4 5.4 2.3 virginica Sepal.Length
#> 150: 5.9 3.0 5.1 1.8 virginica Sepal.Length