在带有其他字符串的嵌套 ifelse 语句中使用 grepl

Using grepl in a nested ifelse statement with other strings

#working example
col1 <- c("cat", "cats", "cat2", "dog", "carrot", "carrots", "broccoli", 
"squash", "tundra", "grassland", "grasslands")
df <- as.data.frame(col1)

我想创建一个新列来标识字符串是动物、植物还是生物群系。

期望的输出:

         col1      col2
1         cat    animal
2        cats    animal
3        cat2    animal
4         dog    animal
5      carrot vegetable
6     carrots vegetable
7    broccoli vegetable
8      squash vegetable
9      tundra     biome
10  grassland     biome
11 grasslands     biome

我想了解为什么以下代码的 grepl 部分不起作用。

df_new <- df %>% mutate(col2 = ifelse(col1 %in% c("dog", grepl("cat", col1)), "animal", 
     ifelse(col1 %in% c(grepl("carrot", col1), "broccoli", "squash"), "vegetable", 
     ifelse(col1 %in% c("tundra", grepl("grassland", col1)), "biome", NA))))

grepl returns一个逻辑向量,需要grep(..., value=TRUE):

df %>% 
    mutate(col2 = ifelse(col1 %in% c("dog", grep("cat", col1, value=T)), "animal", 
                  ifelse(col1 %in% c(grep("carrot", col1, value=T), "broccoli", "squash"), "vegetable", 
                  ifelse(col1 %in% c("tundra", grep("grassland", col1, value=T)), "biome", NA))))

#         col1      col2
#1         cat    animal
#2        cats    animal
#3        cat2    animal
#4         dog    animal
#5      carrot vegetable
#6     carrots vegetable
#7    broccoli vegetable
#8      squash vegetable
#9      tundra     biome
#10  grassland     biome
#11 grasslands     biome