How to find the category with the minimum of observations in a table?

9

Suppose you have the following data:

a<-c(rep("agosto",3),rep("janeiro",4),rep("maio",6))
table(a)

I want to know which month with the least amount of comments?

With the function min(table(a)) the response is the minimum value and not the month with the least observations.

If you use the which.min() function, you get the following result.

> which.min(table(a))
agosto 
     1 

But what I need is to return only the category.

    
asked by anonymous 16.03.2014 / 21:49

1 answer

5

You can only get the names attribute of which.min :

a<-c(rep("agosto",3),rep("janeiro",4),rep("maio",6))
tabela<-table(a)
names(which.min(tabela))
[1] "agosto"

To understand better, when you put which.min(tabela) you generate a vector with an attribute of names:

str(which.min(tabela))
 Named int 1
 - attr(*, "names")= chr "agosto"

So if you want to get only the vector name (s), you use the names function. Another way to do the same thing is attr(which.min(tabela), "names")

    
17.03.2014 / 00:17