filter() then fct_drop() not working

Hi, I want to filter a categorical variable and then drop the unused categories to exclude them from an output table. This is my example from forcats::gss_cat data.

gss_cat %>% filter(relig %in% c("Protestant","Catholic","None")) %>%
fct_drop(relig) %>% sjmisc::frq(relig)

fct_drop is not working with pipe operators and I can not drop unused categories.

Any help would be appreciated.

Hi Seyit, welcome!

You have to use fct_drop() in combination with mutate()

library(dplyr)
library(forcats)

gss_cat <- data.frame(relig = c("Protestant", "Catholic", "None", "Hindu"))

gss_cat %>% 
    filter(relig %in% c("Protestant","Catholic","None")) %>%
    mutate(relig = fct_drop(relig)) %>% 
    sjmisc::frq(relig, show.na = FALSE)
#> 
#> # relig <categorical> 
#> # total N=3  valid N=2  mean=2.00  sd=1.00
#>  
#>         val frq raw.prc valid.prc cum.prc
#>    Catholic   1   33.33     33.33   33.33
#>        None   1   33.33     33.33   66.67
#>  Protestant   1   33.33     33.33  100.00

Created on 2019-03-27 by the reprex package (v0.2.1)

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.