# For each row return the column name of the largest value

0 votes
1 view

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 to return the department name, rather than the number of roster counts, from the frequency table. A simple example below (column names = departments, row names = employee ids).

DF <- matrix(sample(1:9,9),ncol=3,nrow=3)

DF <- as.data.frame.matrix(DF)

> DF

V1 V2 V3

1  2  7  9

2  8  3  6

3  1  5  4

Now how do I get

> DF2

RE

1 V3

2 V1

3 V2

## 1 Answer

0 votes
by (25.4k points)

To return the column name of the largest value for each row, you can use the following

colnames(DF)[apply(DF,1,which.max)]

 "V3" "V1" "V2"

You can also use the max.col function as follows:

colnames(DF)[max.col(DF,ties.method="first")]

 "V3" "V1" "V2"

If you have more than one column that has maximum values, then only the first column name will be printed.i.e.,

colnames(DF)[apply(DF,1,which.max)]

 "V2" "V1" "V2"

Where,

apply(DF,1,function(x) which(x==max(x)))

[]

V2 V3

2  3

[]

V1

1

[]

V2

2

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer