msberends / AMR

Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by using evidence-based methods, as described in https://doi.org/10.18637/jss.v104.i03.
https://msberends.github.io/AMR/
Other
83 stars 12 forks source link

help with %like% #100

Closed iDudeRPS closed 1 year ago

iDudeRPS commented 1 year ago

hai

great update! very significant speed improvement of as.mo !!

I have a question: how can I use %like% to search in a vector ?

for example:

test1 <- c("A. baumannii", "A. bestiarum") test2 <- c("baum", "stiarum")

df %>% filter(microbe_short %in% test1) #I can use this

df %>% filter(microbe_short %like% test2) #but this doesn't work

Is there a way to use %like% to search in a column for matching values?

thanks!

msberends commented 1 year ago

Hi! Thanks for reaching out.

The %like% function works the same as the base R grepl() function - it does not allow different sizes between what you look for (pattern) and where you look in (x). You can do it in two ways:

  1. Use a regular expression to combine the two

    
    df %>% filter(microbe_short %like% "baum|stiarum")
    
    # or:
    test2 <- c("baum", "stiarum")
    df %>% filter(microbe_short %like% paste0(test2, collapse = "|"))
  2. Use if_any() from the dplyr package: https://dplyr.tidyverse.org/reference/across.html

I'm not sure about the second option, but the first should work!

iDudeRPS commented 1 year ago

Thanks!

msberends commented 1 year ago

I assume this can be closed 😊