首页 > 解决方案 > r中条件str_replace_all的大小写敏感性

问题描述

例如我有这个代码。

# Lookup List
fruits <- c("guava","avocado", "apricots","kiwifruit","banana")
vegies <- c("tomatoes", "asparagus", "peppers", "broccoli", "leafy greens")

# Patterns
foods <- c("guava", "banana", "broccoli")
patterns <- str_c(foods, collapse="|")

# Sample Sentence
sentence <- "I want to eat banana and broccoli!" 


typeOfFood <- function(foods) {

  if( foods %in% fruits ){
    type <- "FRUITS"
  }
  else if( foods %in% vegies ){
    type <- "VEGIES"
  }
  paste0(foods,"(",type,")")

}

str_replace_all(sentence, patterns, typeOfFood)

输出:

[1] "I want to eat banana(FRUITS) and broccoli(VEGIES)!"

我想在不使用 tolower(sentence) 的情况下忽略区分大小写。

例句:

sentence <- "I want to eat BANANA and Broccoli!"

样本输出:

[1] "I want to eat BANANA(FRUITS) and Broccoli(VEGIES)!"

标签: rregex

解决方案


您可以使用具有选项的regex()辅助函数。stringrignore_case

您将需要修改typeOfFood以忽略大小写。

typeOfFood <- function(foods) {
  if(tolower(foods) %in% fruits ){
    type <- "FRUITS"
  }
  else if(tolower(foods) %in% vegies ){
    type <- "VEGIES"
  }
  paste0(foods,"(",type,")")
}

sentence <- "I want to eat BANANA and Broccoli!"
str_replace_all(sentence, regex(patterns, ignore_case = TRUE), typeOfFood)
# [1] "I want to eat BANANA(FRUITS) and Broccoli(VEGIES)!"

推荐阅读