One possible method is to use the case_when() function from dplyr combined with vectors defining which animals are in which class.
library(dplyr)
DF <- data.frame(Animal=c("Elephant","Snake","Crocodile","Dog"),
Values=1:4)
DF
#> Animal Values
#> 1 Elephant 1
#> 2 Snake 2
#> 3 Crocodile 3
#> 4 Dog 4
MAMMALS <- c("Elephant", "Dog")
REPTILES <- c("Crocodile", "Snake")
DF<- DF |> mutate(Animal = case_when(
Animal %in% MAMMALS ~ "Mammal",
Animal %in% REPTILES ~ "Reptile",
TRUE ~ "Not Found"
))
DF
#> Animal Values
#> 1 Mammal 1
#> 2 Reptile 2
#> 3 Reptile 3
#> 4 Mammal 4
Created on 2022-07-26 by the reprex package (v2.0.1)