【问题标题】:Using grepl in a nested ifelse statement with other strings在带有其他字符串的嵌套 ifelse 语句中使用 grepl
【发布时间】: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 %&gt;% mutate(col2 = case_when(grepl('cat|dog', col1) ~ 'animal', grepl('carrot|broccoli|squash', col1) ~ 'vegetable', grepl('tundra|grassland', col1) ~ 'biome'))或者查表比较简单

标签: r dplyr grepl


【解决方案1】:

grepl返回一个逻辑向量,你需要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

【讨论】:

  • 非常感谢。我试过 grep 而不是 grepl 但不知道 value=T。
猜你喜欢
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 2018-10-25
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多