What about this?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- data.frame(stringsAsFactors = FALSE,
Var1 = c("apples", "cats", "green cats", "green apples", "red cats"))
df %>%
mutate(Var2 = case_when(grepl("apples", Var1) ~ "fruit",
grepl("cats", Var1) ~ "animal"))
#> Var1 Var2
#> 1 apples fruit
#> 2 cats animal
#> 3 green cats animal
#> 4 green apples fruit
#> 5 red cats animal
Created on 2019-03-29 by the reprex package (v0.2.1)
You can also use if - else if - else