2

I want to find the maximum value in each row across three columns and have the new column print the name of the highest value column.

An example table would appear as follows:

x = c(1,2,5,4,5 ) 
y = c(2,3,3,1,1 )
z = c(4,4,2,1,1 )
df<-data.frame(x,y,z)

I want to create this:

id x y z max
1 1 2 4 z
2 2 3 4 z
3 5 3 2 x
4 4 1 1 x
5 5 1 1 x

I tried:

df%>% rowwise() %>% mutate(max = max(x, y, z))

And received the output:

id x y z max
1 1 2 4 4
2 2 3 4 4
3 5 3 2 5
4 4 1 1 4
5 5 1 1 5

Does anybody know how I can correct this code to yield the desired outcome?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user16004454
  • 21
  • 1
  • 3

1 Answers1

5

Use pmax instead of max as it vectorized and doesn't need to go through rowwise (which should be very slow)

library(dplyr)
df %>% 
    mutate(Max = pmax(x, y, z))

In the OP's function with rowwise, if we need the column name, use which.max to get the column index

df %>%
  rowwise %>%
  mutate(Max = names(.)[which.max(c(x, y, z))]) %>%
  ungroup

-output

# A tibble: 5 × 4
      x     y     z Max  
  <dbl> <dbl> <dbl> <chr>
1     1     2     4 z    
2     2     3     4 z    
3     5     3     2 x    
4     4     1     1 x    
5     5     1     1 x    

Or use vectorizeid max.col to find the column index of each row with the max value and get the names corresponding to that index

df %>% 
    mutate(Max = names(.)[max.col(., 'first')])

-output

  x y z Max
1 1 2 4   z
2 2 3 4   z
3 5 3 2   x
4 4 1 1   x
5 5 1 1   x
akrun
  • 874,273
  • 37
  • 540
  • 662