kenda
kenda

Reputation: 488

Plot factor by frequency

I have a factor a:

> a
[1]  foo                                                    
[2]  bar                      
[3]  foo                                
[4]  baz                                                     
[5]  bar
[6]  foo

I want to plot() this factor but only including levels with a minimum frequency of two. So that only foo and bar are plotted, not baz.

How can I achive this?

Upvotes: 2

Views: 4850

Answers (2)

plannapus
plannapus

Reputation: 18749

Another solution was to use function summary in combination with as.factor:

summary(as.factor(a)) -> b
barplot(b[b>=2])

Upvotes: 2

csgillespie
csgillespie

Reputation: 60462

You just use standard subsetting. First, create some table:

d = factor(sample(LETTERS, 50, replace=TRUE))

next, create a table of frequencies:

freq_tab = table(d)

Finally, subset and plot:

barplot(freq_tab[freq_tab>=2])

Upvotes: 7

Related Questions