Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 选择除带加号正则表达式的字符串以外的所有内容_R_Regex_Stringr_Regex Negation - Fatal编程技术网

R 选择除带加号正则表达式的字符串以外的所有内容

R 选择除带加号正则表达式的字符串以外的所有内容,r,regex,stringr,regex-negation,R,Regex,Stringr,Regex Negation,假设我在R中有以下数据帧: df <- tribble( ~id, ~key, 1, "+999..3762962", 2, "0677219-30911", 3, "-739812//3918", 4, "+273$$8279838", 5, "1904-03940538", 6, NA ) 它不起作用,它最终会过滤掉除第6行之外的

假设我在R中有以下数据帧:

df <- tribble(
    ~id, ~key,
    1, "+999..3762962",
    2, "0677219-30911",
    3, "-739812//3918",
    4, "+273$$8279838",
    5, "1904-03940538",
    6, NA
)
它不起作用,它最终会过滤掉除第6行之外的所有缺失值

我做错了什么?我尝试过搜索类似的问题,但它们要求使用regex进行非常具体的选择,因此,生成的代码/建议对我来说几乎是无法理解的。我相信答案很简单


谢谢。

我们可以使用str_detect搜索字符串中是否存在+字符,并从@thelatemail中指定negate=TRUE。在这里,| with is.na-用于返回缺少值的行-默认情况下,过滤器会删除这些na行

带grep的基本R版本:


谢谢我在找你的第二个答案。我不知道我必须在开始时至少使用^[^+]来过滤掉加号str\u detect还有一个negate=参数-df%>%filterstr\u detectkey,\\+,negate=TRUE@caproki代码中的问题是[^\\+]它匹配任何非a+的字符,并且在除NA之外的所有情况下都返回TRUE
library(tidyverse)

df %>% 
    filter(str_detect(key, "[^\\+]"))
library(dplyr)
library(stringr)
df %>% 
   filter(str_detect(key, fixed('+'), negate = TRUE)|is.na(key))
# A tibble: 4 x 2
#    id key          
#  <dbl> <chr>        
#1     2 0677219-30911
#2     3 -739812//3918
#3     5 1904-03940538
#4     6 <NA>         
df %>% 
   filter(str_detect(key, '^[^+]+$')|is.na(key))
# A tibble: 4 x 2
#     id key          
#  <dbl> <chr>        
#1     2 0677219-30911
#2     3 -739812//3918
#3     5 1904-03940538
#4     6 <NA>        
df[grep('+', df$key, fixed = TRUE, invert = TRUE),]

#     id key          
#  <dbl> <chr>        
#1     2 0677219-30911
#2     3 -739812//3918
#3     5 1904-03940538
#4     6 NA