在 R 中,stringr:str_replace_all 不服从 'ignore_case = TRUE'
In R, stringr:str_replace_all disobeying 'ignore_case = TRUE'
这是 str_replace_all
中的错误还是我真的误会了什么?
library(tidyverse)
packageVersion("tidyverse")
# [1] ‘1.2.1’
R.version.string
# [1] "R version 3.5.2 (2018-12-20)"
fruits <- c("one apple", "two pears", "three bananas")
这按预期工作:
fruits %>%
str_replace_all(c("on\w+" = "1", "two" = "2", "three" = "3"))
# [1] "1 apple" "2 pears" "3 bananas"
这也是:
fruits %>%
str_replace_all(c(regex("on\w+", ignore_case = TRUE), "two", "three"),
c("1", "2", "3"))
# [1] "1 apple" "2 pears" "3 bananas"
但是当我试图让它区分大小写时,它没有使用 ignore_case
:
fruits %>%
str_replace_all(c(regex("ON\w+", ignore_case = TRUE), "two", "three"),
c("1", "2", "3"))
# [1] "one apple" "2 pears" "3 bananas"
问题似乎出在 str_replace_all
而不是 regex
fruits %>%
str_detect(regex("ON\w+", ignore_case = TRUE))
# [1] TRUE FALSE FALSE
为了我的目的,我有一个解决方法,但是 - 有什么见解吗?
我们可以使用(?i)
library(dplyr)
library(stringr)
fruits %>%
str_replace_all(c("(?i)ON\w+", "two", "three"), as.character(1:3))
#[1] "1 apple" "2 pears" "3 bananas"
或者把所有东西都包在里面 regex
fruits %>%
str_replace_all(regex(c("ON\w+", "two", "three"),
ignore_case = TRUE), c('1', '2', '3'))
#[1] "1 apple" "2 pears" "3 bananas"
这是 str_replace_all
中的错误还是我真的误会了什么?
library(tidyverse)
packageVersion("tidyverse")
# [1] ‘1.2.1’
R.version.string
# [1] "R version 3.5.2 (2018-12-20)"
fruits <- c("one apple", "two pears", "three bananas")
这按预期工作:
fruits %>%
str_replace_all(c("on\w+" = "1", "two" = "2", "three" = "3"))
# [1] "1 apple" "2 pears" "3 bananas"
这也是:
fruits %>%
str_replace_all(c(regex("on\w+", ignore_case = TRUE), "two", "three"),
c("1", "2", "3"))
# [1] "1 apple" "2 pears" "3 bananas"
但是当我试图让它区分大小写时,它没有使用 ignore_case
:
fruits %>%
str_replace_all(c(regex("ON\w+", ignore_case = TRUE), "two", "three"),
c("1", "2", "3"))
# [1] "one apple" "2 pears" "3 bananas"
问题似乎出在 str_replace_all
而不是 regex
fruits %>%
str_detect(regex("ON\w+", ignore_case = TRUE))
# [1] TRUE FALSE FALSE
为了我的目的,我有一个解决方法,但是 - 有什么见解吗?
我们可以使用(?i)
library(dplyr)
library(stringr)
fruits %>%
str_replace_all(c("(?i)ON\w+", "two", "three"), as.character(1:3))
#[1] "1 apple" "2 pears" "3 bananas"
或者把所有东西都包在里面 regex
fruits %>%
str_replace_all(regex(c("ON\w+", "two", "three"),
ignore_case = TRUE), c('1', '2', '3'))
#[1] "1 apple" "2 pears" "3 bananas"