user7892705
user7892705

Reputation: 147

Drop unused levels from a factor after filtering data frame using dplyr

I used dplyr function to create a new data sets which contain the names that have less than 4 rows.

df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9)

aa = df %>%
    group_by(name) %>%
    filter(n() < 4)

But when I type

table(aa$name)

I get,

a b c 
3 2 0 

I would like to have my output as follow

a b 
3 2 

How to completely separate new frame aa from df?

Upvotes: 5

Views: 5664

Answers (2)

mpalanco
mpalanco

Reputation: 13580

To complete your answer and KoenV's comment you can just, write your solution in one line or apply the function factor will remove the unused levels:

table(droplevels(aa$name))
table(factor(aa$name))

or because you are using dplyr add droplevels at the end:

aa <- df %>%
       group_by(name) %>%
       filter(n() < 4) %>% 
       droplevels()
table(aa$name)

# Without using table
df %>%
  group_by(name) %>%
  summarise(count = n()) %>% 
  filter(count < 4)

Upvotes: 7

user7892705
user7892705

Reputation: 147

 aaNew <- droplevels(aa)
 table(aa$name)

Upvotes: 0

Related Questions