Looking for easier way to standardize genus names.

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
))

Since you only need the genus, I would first pull out the first word with word() from stringr and clean it up, then handle the few spelling variants in a small lookup table joined back to the data. That keeps the main code short and makes new variants easy to add without growing another long case_when.

You can replace the long case_when() with a vector of genus names and a single str_extract():

library(dplyr)
library(stringr)

genera <- c("Anabaena", "Aphanizomenon", "Microcystis", "Planktothrix")  # add the rest

df <- df %>%
  mutate(`Dominant Taxa` = coalesce(
    str_extract(`Dominant Taxa`, str_c(genera, collapse = "|")),
    `Dominant Taxa`
  ))

This also avoids retyping each output name. Your current code has typos there ("Limnnothrix", "Chroococus", "Woronichina"). Please also check "Gleotrichia", as the usual spelling is "Gloeotrichia". if u still have a probleme contact me i will help u