For each row return the column name of the largest value

前端 未结 8 2284
礼貌的吻别
礼貌的吻别 2020-11-21 07:06

I have a roster of employees, and I need to know at what department they are in most often. It is trivial to tabulate employee ID against department name, but it is trickier

8条回答
  •  一整个雨季
    2020-11-21 07:35

    One option from dplyr 1.0.0 could be:

    DF %>%
     rowwise() %>%
     mutate(row_max = names(.)[which.max(c_across(everything()))])
    
         V1    V2    V3 row_max
           
    1     2     7     9 V3     
    2     8     3     6 V1     
    3     1     5     4 V2     
    

    Sample data:

    DF <- structure(list(V1 = c(2, 8, 1), V2 = c(7, 3, 5), V3 = c(9, 6, 
    4)), class = "data.frame", row.names = c(NA, -3L))
    

提交回复
热议问题