Search code examples
rphyloseq

change genus string of specific taxa in phyloseq object


I have a phyloseq where I need to change all the genera starting with Candidatus, from Candidatus to C., while leaving the other genera name in full name. I can give and example phyloseq:

data("GlobalPatterns")
taxa_table = as.data.frame(GlobalPatterns@tax_table)

but when I try to do this:

tax_table$Species <- sub(tax_table$Species, grepl("Candidatus",tax_table$Species), "C.")

everything converts to C. how can I rename the string Candidatus with C. using sub? thanks a lot


Solution

  • taxa_table$Species <- sub("^Candidatus", "C.", taxa_table$Species)
    

    The ^ signifies "beginning of string", in case "Candidatus" happens to appear somewhere else in the taxon names (unless you want it converted wherever it occurs, in which case leave it out).

    You can check the results with

    unique(taxa_table$Species) |> grep(pattern="^C", value = TRUE)