Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop unused levels after filtering by factor? [duplicate]

Tags:

r

dplyr

Here is an example that was taken from a fellow SO member.

# define a %not% to be the opposite of %in%
library(dplyr)
# data
f <- c("a","a","a","b","b","c")
s <- c("fall","spring","other", "fall", "other", "other")
v <- c(3,5,1,4,5,2)
(dat0 <- data.frame(f, s, v))
#  f      s v
#1 a   fall 3
#2 a spring 5
#3 a  other 1
#4 b   fall 4
#5 b  other 5
#6 c  other 2
(sp.tmp <- filter(dat0, s == "spring"))
#  f      s v
#1 a spring 5
(str(sp.tmp))
#'data.frame':  1 obs. of  3 variables:
# $ f: Factor w/ 3 levels "a","b","c": 1
# $ s: Factor w/ 3 levels "fall","other",..: 3
# $ v: num 5

The df resulting from filter() has retained all the levels from the original df.

What would be the recommended way to drop the unused level(s), i.e. "fall" and "others", within the dplyr framework?

like image 719
ils Avatar asked Nov 09 '14 09:11

ils


People also ask

How do you drop unused levels of a factor 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.

How do you remove a level from a factor?

Removing Levels from a Factor in R Programming – droplevels() Function. droplevels() function in R programming used to remove unused levels from a Factor. droplevels(x, exclude = if(anyNA(levels(x))) NULL else NA, …)

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") .


2 Answers

You could do something like:

dat1 <- dat0 %>%
  filter(s == "spring") %>% 
  droplevels()

Then

str(df)
#'data.frame':  1 obs. of  3 variables:
# $ f: Factor w/ 1 level "a": 1
# $ s: Factor w/ 1 level "spring": 1
# $ v: num 5
like image 196
talat Avatar answered Oct 10 '22 14:10

talat


You could use droplevels

 sp.tmp <- droplevels(sp.tmp)
 str(sp.tmp)
 #'data.frame': 1 obs. of  3 variables:
 #$ f: Factor w/ 1 level "a": 1
 #$ s: Factor w/ 1 level "spring": 1
# $ v: num 5
like image 27
akrun Avatar answered Oct 10 '22 13:10

akrun