How to find a table by category with minimum observations?

Asked

Viewed 159 times

9

Suppose you have the following data:

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

I want to know the month with the minimum of observations?

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

If you use the function which.min() the following result shall be obtained.

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

But what I need is for you to return only to the category.

1 answer

5


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

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

To better understand, when you put which.min(tabela) you generate an array with a name attribute:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.