Search code examples
rdatabasedataframemax

How to get the maximum count of data in string format


This is my code:

   table(Car_DataSet_no_na$Dealer)

Output:

> table(Car_DataSet_no_na$Dealer)
 
  Anny David Henry 
  870   263   128 

I assigned it as (x) and want to to get the maximum Dealer:

> x<-table(Car_DataSet_no_na$Dealer)
> max(x)
[1] 870

It gives me only the number,How can I get the number and name of the Dealer?

Thank you in advance!


Solution

  • I will explain the answer of Adam Quek in a reproducible example:

    exampleVector <- sample(x = rep(x = c("Pikachu", "Charmander", "Balbasaur"), 
                                    times = c(30, 15, 45)))
    
    x <- table(exampleVector)
    
    # The number
    max(x)
    
    # The name
    names(x)[which.max(x)]
    

    Is it what you looking for?