slartidan
slartidan

Reputation: 21608

How to count duplicate entries in a vector?

I have a vector like this:

v = c('Max', 'Max', 'Sven', 'Bob')

How can I count the occurances for all items? The result should contain unique labels, each with its number of occurances. In the example, this could look like this:

> result
$Max
[1] 2

$Sven
[1] 1

$Bob
[1] 1

Upvotes: 1

Views: 107

Answers (1)

slartidan
slartidan

Reputation: 21608

You can use this:

table(v)

or, if the desired output format is a list, use:

as.list(table(v))

Thanks @AntoniosK for providing this answer in a comment

Upvotes: 2

Related Questions