Home > Software design >  How to get the maximum count of data in string format
How to get the maximum count of data in string format

Time:07-09

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!

CodePudding user response:

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?

CodePudding user response:

Yes well that's simple you can use the max(x) for showing the number and use the example<- which.max(x) to show the string format or (name).

  • Related