R的新手。
我正在尝试变异(Location2),但它给了我警告消息:
#fix truncated values
DFnew %>%
mutate(location2 = case_when(
str_starts(Location, c("s", "S", "S.")) ~ "S. SJ @ Ashley Store",
str_starts(Location, c("p", "P")) ~ "Pleasanton @ Ranch 99",
TRUE ~ NA_character_
))
Warning messages:
1: Problem with `mutate()` input `location2`.
i longer object length is not a multiple of shorter object length
i Input `location2` is `case_when(...)`.
2: In stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) :
longer object length is not a multiple of shorter object length发布于 2020-12-10 11:47:57
str_starts或任何stringr函数都接受正则表达式模式。如果你传递一个向量,它将匹配第一个模式和第一个值,第二个模式和第二个值,依此类推。
所以试试吧:
library(dplyr)
library(stringr)
DFnew <- DFnew %>%
mutate(location2 = case_when(
str_starts(Location, "s|S|S\\.") ~ "S. SJ @ Ashley Store",
str_starts(Location, "p|P") ~ "Pleasanton @ Ranch 99",
TRUE ~ NA_character_
))或者,您也可以使用带有ignore_case = TRUE的regex()函数来使正则表达式不区分大小写。
DFnew <- DFnew %>%
mutate(location2 = case_when(
str_starts(Location, regex("S\\.?", ignore_case = TRUE)) ~ "S. SJ @ Ashley Store",
str_starts(Location, regex("P", ignore_case = TRUE)) ~ "Pleasanton @ Ranch 99",
TRUE ~ NA_character_
))https://stackoverflow.com/questions/65228215
复制相似问题