0

I want to get the column name of the largest number of each row. for example, A table could be

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

the results what I want to get is

  x y z cloumn_name
1 1 2 4 z
2 2 3 5 z
3 4 3 2 x
4 4 5 1 y
5 3 4 1 y
AdIan
  • 125
  • 1
  • 6

4 Answers4

3

Try this with base R and apply

df$maxcol <- apply(df, 1, function(x) colnames(df)[which.max(x)])
> df$maxcol
[1] "z" "z" "x" "y" "y"

This is quite a simple solution, as you can see the margin parameter of apply is set to 1 which means that the function will be applied for all rows of df.

gaut
  • 5,771
  • 1
  • 14
  • 45
2

Does this work:

library(dplyr)
df %>% rowwise() %>% mutate(column_name = names(df)[which.max(c_across())])
# A tibble: 5 x 4
# Rowwise: 
      x     y     z column_name
  <dbl> <dbl> <dbl> <chr>      
1     1     2     4 z          
2     2     3     5 z          
3     4     3     2 x          
4     4     5     1 y          
5     3     4     1 y      
Karthik S
  • 11,348
  • 2
  • 11
  • 25
2

This can also be reached reshaping data, but it can be a bit long:

library(dplyr)
library(tidyr)
#Code
df <- df %>% mutate(id=row_number()) %>%
  left_join(
    df %>% mutate(id=row_number()) %>%
      pivot_longer(-id) %>%
      group_by(id) %>%
      mutate(Var=ifelse(value==max(value),name,NA)) %>%
      select(c(id,Var)) %>%
      filter(!is.na(Var)) %>%
      filter(!duplicated(Var)) 
  ) %>% select(-id)

Output:

  x y z Var
1 1 2 4   z
2 2 3 5   z
3 4 3 2   x
4 4 5 1   y
5 3 4 1   y
Duck
  • 39,058
  • 13
  • 42
  • 84
2

Here is a vectorised solution:

library(dplyr)
df %>% mutate(column_name = names(df)[apply(df,1,which.max)])
Marcos Pérez
  • 1,260
  • 2
  • 7