R: Cut equivalent for strings

I have a list of different types of masks and I would like to categorise them into those that are N95, surgical, cloth or other.

     df<-data.frame(mask_type=
c("Surgical Mask (3M 1800)",                                                                      
     "N95 FFR (Wilson 1105N) (2x 3mm leaks)",                                                        
     "N95 FFR (San Huei United Company 1895N) (2x 3mm leaks)",                                       
     "Surgical Mask (Primed  PG4-1073) (2x 3mm leaks)",                                              
     "Surgical Mask (3M 1800) (2x 3mm leaks)",                                                       
     "N95 FFR (Wilson 1105N) (4x 3mm leaks)",                                                        
     "Cloth FFR (San Huei United Company 1895N) (4x 3mm leaks)",                                       
     "Cloth Mask (Primed  PG4-1073) (4x 3mm leaks)") 

This works that filters the masks but doesn't create an "other" column. Am I far away do you think?

require(dplyr) require(tidyr)

df %>%
  mutate(TYPE=stringr::str_detect(mask_type,"N95 | surgical | cloth")) %>%
  filter(TYPE=TRUE) %>%
  select(mask_type)


Read more here: https://stackoverflow.com/questions/66275681/r-cut-equivalent-for-strings

Content Attribution

This content was originally published by HCAI at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: