I have a dataset that includes taxa names that are not standardly formatted or have multiple species included. I want only the genus to be included for the whole column. The way I've performed this is to use mutate(case(when(str_detect())) for each taxa listed in the data. This took forever and the code looks bulky. Is there an easier way to do this for future reference?
This is my code for reference:
mutate(
Anatoxin-a (ug/L) = case_when(
Anatoxin-a (ug/L) < 0.171 ~ Anatoxin-a (ug/L) / 2,
.default = as.numeric(Anatoxin-a (ug/L)))) |>
mutate(Dominant Taxa = case_when(
str_detect(Dominant Taxa, fixed("Anabaena", ignore_case = FALSE)) ~ "Anabaena",
str_detect(Dominant Taxa, fixed("Aphanizomenon", ignore_case = FALSE)) ~ "Aphanizomenon",
str_detect(Dominant Taxa, fixed("Aphanocapsa", ignore_case = FALSE)) ~ "Aphanocapsa",
str_detect(Dominant Taxa, fixed("Calothrix", ignore_case = FALSE)) ~ "Calothrix",
str_detect(Dominant Taxa, fixed("Limnothrix", ignore_case = FALSE)) ~ "Limnnothrix",
str_detect(Dominant Taxa, fixed("Synechococcus", ignore_case = FALSE)) ~ "Synechococcus",
str_detect(Dominant Taxa, fixed("Chroococcus", ignore_case = FALSE)) ~ "Chroococus",
str_detect(Dominant Taxa, fixed("Coelosphaerium", ignore_case = FALSE)) ~ "Coelosphaerium",
str_detect(Dominant Taxa, fixed("Cylindrospermopsis", ignore_case = FALSE)) ~ "Cylindrospermopsis",
str_detect(Dominant Taxa, fixed("Dolichospermum", ignore_case = FALSE)) ~ "Dolichospermum",
str_detect(Dominant Taxa, fixed("Eucapsis", ignore_case = FALSE)) ~ "Eucapsis",
str_detect(Dominant Taxa, fixed("Gleotrichia", ignore_case = FALSE)) ~ "Gleotrichia",
str_detect(Dominant Taxa, fixed("Heteroleibleinia", ignore_case = FALSE)) ~ "Heteroleibleinia",
str_detect(Dominant Taxa, fixed("Leptolyngbya", ignore_case = FALSE)) ~ "Leptolyngbya",
str_detect(Dominant Taxa, fixed("Lyngbya", ignore_case = FALSE)) ~ "Lyngbya",
str_detect(Dominant Taxa, fixed("Merismopedia", ignore_case = FALSE)) ~ "Merismopedia",
str_detect(Dominant Taxa, fixed("Microcoleus", ignore_case = FALSE)) ~ "Microcoleus",
str_detect(Dominant Taxa, fixed("Microcystis", ignore_case = FALSE)) ~ "Microcystis",
str_detect(Dominant Taxa, fixed("Nostoc", ignore_case = FALSE)) ~ "Nostoc",
str_detect(Dominant Taxa, fixed("Phormidium", ignore_case = FALSE)) ~ "Phormidium",
str_detect(Dominant Taxa, fixed("Planktolyngbya", ignore_case = FALSE)) ~ "Planktolyngbya",
str_detect(Dominant Taxa, fixed("Planktothrix", ignore_case = FALSE)) ~ "Planktothrix",
str_detect(Dominant Taxa, fixed("Pseudanabaena", ignore_case = FALSE)) ~ "Pseudanabaena",
str_detect(Dominant Taxa, fixed("Spirulina", ignore_case = FALSE)) ~ "Spirulina",
str_detect(Dominant Taxa, fixed("Tolypothrix", ignore_case = FALSE)) ~ "Tolypothrix",
str_detect(Dominant Taxa, fixed("Woronichinia", ignore_case = FALSE)) ~ "Woronichina",
.default = Dominant Taxa
))