0

I have a huge database of that lists antibiotic resistance genes as part of a variable in a dataframe. I want to write a script that automatically assigns the correct antibiotic group the gene belongs to, to a new corresponding variable. For example:

    Nr  Gene
[1] 1  "TEM-1, CTX-M-12, CTX-M-14, ampC"
[2] 2  "VIM, blaCMY, CTX-M-24"

Now if, for example, I were to assign all genes containing "CTX" and "CMY" to beta-lactamases, and "VIM" to Vancomycin, it should return:

    Nr  Gene                               Group
[1] 1   "TEM-1, CTX-M-12, CTX-M-14, ampC"  "beta-lactamases"
[2] 2   "VIM, blaCMY, CTX-M-24"            "beta-lactamases, Vancomycin"

I have tried using the if function:

if ("*CTX*" or "*CMY*" %in% df$Gene) {
  df$Group <- paste("beta-lactamases")
}

But does not work. Any help would be appreciated.

  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – Sotos Apr 19 '18 at 09:20
  • You need to use `grepl` function in your `if` statements, e.g. `grepl("CTX", "TEM-1, CTX-M-12, CTX-M-14, ampC")` returns `TRUE` – pieca Apr 19 '18 at 09:26

0 Answers0