get column names rowwise which contains largest value across multiple columns

How can I get column names rowwise which contains largest value across multiple columns removing NA?

For example, I would like to mutate column 'Max' from columns 'A: C' in dplyr chain.

df <- tibble(A=c(1,  2,  3, 4, 5),
             B=c(2,  1, NA, 5, 4),
             C=c(NA, 3,  5, 2, 4),
             Max=c('B', 'C', 'C', 'B', 'A')) 

print(df)

 > # A tibble: 5 × 4
       A     B     C Max  
   <dbl> <dbl> <dbl> <chr>
1     1     2    NA B    
2     2     1     3 C    
3     3    NA     5 C    
4     4     5     2 B    
5     5     4     4 A    

I would use which.max() and colnames().

library(tidyverse)
df <- tibble(A=c(1,  2,  3, 4, 5),
             B=c(2,  1, NA, 5, 4),
             C=c(NA, 3,  5, 2, 4),
             Max=c('B', 'C', 'C', 'B', 'A')) 
df |> rowwise() |> 
  mutate(Max2 = colnames(df)[which.max(c_across(A:C))])
#> # A tibble: 5 × 5
#> # Rowwise: 
#>       A     B     C Max   Max2 
#>   <dbl> <dbl> <dbl> <chr> <chr>
#> 1     1     2    NA B     B    
#> 2     2     1     3 C     C    
#> 3     3    NA     5 C     C    
#> 4     4     5     2 B     B    
#> 5     5     4     4 A     A

Created on 2023-12-18 with reprex v2.0.2

1 Like

Thanks for your quick response and excellent solution :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.