For each row return the column name of the largest value

前端 未结 8 2296
礼貌的吻别
礼貌的吻别 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:42

    Here is an answer that works with data.table and is simpler. This assumes your data.table is named yourDF:

    j1 <- max.col(yourDF[, .(V1, V2, V3, V4)], "first")
    yourDF$newCol <- c("V1", "V2", "V3", "V4")[j1]
    

    Replace ("V1", "V2", "V3", "V4") and (V1, V2, V3, V4) with your column names

提交回复
热议问题