【发布时间】:2017-12-06 22:33:32
【问题描述】:
#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))))
【问题讨论】:
-
用
case_when:df %>% mutate(col2 = case_when(grepl('cat|dog', col1) ~ 'animal', grepl('carrot|broccoli|squash', col1) ~ 'vegetable', grepl('tundra|grassland', col1) ~ 'biome'))或者查表比较简单