3

Let's say that my data frame contains

> DF
   V1   V2   V3  
1  0.3  0.4  0.7  
2  0.4  0.2  0.1  
3  0.2  0.8  0.3  
4  0.5  0.8  0.9   
5  0.2  0.7  0.8  
6  0.8  0.3  0.6  
7  0.1  0.5  0.4  

the rows would be the different types of automobiles, and the columns would be the probabilities for a given category of V1, V2, V3.

I want to generate a vector that assigns to each automobile the category of which it has the highest probability in. For example, I'd want automobile 1 to be associated with V3, automobile 2 to be associated with V1, automobile 3 to be associated with V2.

Any aids or hints on how I should tackle this?

RHertel
  • 23,412
  • 5
  • 38
  • 64
Elizabeth
  • 71
  • 2
  • 9

2 Answers2

4

We can use max.col to get the column index that corresponds to the largest value in each row.

names(DF)[max.col(DF, "first")]
#[1] "V3" "V1" "V2" "V3" "V3" "V1" "V2"
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another solution is:

names(DF)[apply(DF, 1, which.max)]
# [1] "V3" "V1" "V2" "V3" "V3" "V1" "V2"
FlorianSchunke
  • 571
  • 5
  • 15