首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >how to mutate()-警告消息

how to mutate()-警告消息
EN

Stack Overflow用户
提问于 2020-12-10 11:43:26
回答 1查看 54关注 0票数 0

R的新手。

我正在尝试变异(Location2),但它给了我警告消息:

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-10 11:47:57

str_starts或任何stringr函数都接受正则表达式模式。如果你传递一个向量,它将匹配第一个模式和第一个值,第二个模式和第二个值,依此类推。

所以试试吧:

代码语言:javascript
复制
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 = TRUEregex()函数来使正则表达式不区分大小写。

代码语言:javascript
复制
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_
  ))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65228215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档