Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

plyr

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?

like image 955
user7892705 Avatar asked Aug 23 '17 07:08

user7892705


People also ask

How do you get rid of unused factors in R?

The droplevels() function in R can be used to drop unused factor levels. This function is particularly useful if we want to drop factor levels that are no longer used due to subsetting a vector or a data frame. where x is an object from which to drop unused factor levels.

What does Droplevels mean in R?

droplevels in R with examples, To remove unneeded factor levels, use R's droplevels() function. This function comes in handy when we need to get rid of factor levels that are no longer in use as a result of subsetting a vector or a data frame. The syntax for this function is as follows. droplevels(x) droplevels(x)

How do you change factor levels in R?

How do I Rename Factor Levels in R? The simplest way to rename multiple factor levels is to use the levels() function. For example, to recode the factor levels “A”, “B”, and “C” you can use the following code: levels(your_df$Category1) <- c("Factor 1", "Factor 2", "Factor 3") .


1 Answers

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)
like image 87
mpalanco Avatar answered Dec 28 '22 21:12

mpalanco