r - 如何变异() - 警告信息
问题描述
R的新手。
我正在尝试 mutate(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
解决方案
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_
))
或者您也可以使用regex()
函数 withignore_case = TRUE
使正则表达式不区分大小写。
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_
))
推荐阅读
- ubuntu - 使用 grafonnet-lib 运行 jsonnet 时没有此类文件或目录错误
- r - 将 rowSums 的结果分配给 R 中的新列
- angular - 获取模板中的 Angular formArrayName 索引值
- api - 如何在 Swagger 中创建内部和外部文档?
- c++ - 使用 500MB RAM 的 10MB 图像
- java - Nifi处理中缺少流文件异常导致信息丢失
- javascript - JS 自动滚动代码仅用作 HTML 的一部分,但不能用作 JSFiddle 中的单独脚本
- javascript - 格式化日期数组
- visual-studio-code - 使用带有 remote-ssh 扩展的 Visual Studio Code 时设置环境变量
- android - Profiler 中的内存使用量远低于实际设备