Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
使用stringr'过滤字符向量的行;s str_detect()_R_Character Encoding_Dplyr_Stringr_Magrittr - Fatal编程技术网

使用stringr'过滤字符向量的行;s str_detect()

使用stringr'过滤字符向量的行;s str_detect(),r,character-encoding,dplyr,stringr,magrittr,R,Character Encoding,Dplyr,Stringr,Magrittr,我正试图使用捕获两个或更多数字的正则表达式,使用dplyr::filter()、stringr::stru detect和magrittr-管道对字符列a进行子集划分 这似乎仅适用于数字列,并且仅当使用$-运算符直接访问该列时: library(tidyverse) # Create example data: test_num <- tibble( a = c(1:3, 22:24)) test_num #> # A tibble: 6 x 1 #> a

我正试图使用捕获两个或更多数字的正则表达式,使用
dplyr::filter()
stringr::stru detect
magrittr
-管道对字符列
a
进行子集划分

这似乎仅适用于数字列,并且仅当使用
$
-运算符直接访问该列时:

library(tidyverse)

# Create example data: 
test_num <- tibble(
  a = c(1:3, 22:24))
test_num
#> # A tibble: 6 x 1
#>       a
#>   <int>
#> 1     1
#> 2     2
#> 3     3
#> 4    22
#> 5    23
#> 6    24

test_char <- tibble(
  a = as.character(c(1:3, 22:24)))
test_char 
#> # A tibble: 6 x 1
#>   a    
#>   <chr>
#> 1 1    
#> 2 2    
#> 3 3    
#> 4 22   
#> 5 23   
#> 6 24

# Subsetting numerical columns works:
test_num %>% 
  dplyr::filter(a, stringr::str_detect(a, "\\d{2,}"))
#> # A tibble: 3 x 1
#>       a
#>   <int>
#> 1    22
#> 2    23
#> 3    24

# Subsetting a character columns does not work:
test_char %>% 
  dplyr::filter(a, stringr::str_detect(a, "\\d{2,}"))
#> Error in filter_impl(.data, quo): Evaluation error: operations are possible only for numeric, logical or complex types.

# Wheras subsetting by accessing the column
# using the `$` operator works:
test_char$a %>% 
  stringr::str_detect("\\d{2,}")
#> [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

test_num$a %>% 
  stringr::str_detect("\\d{2,}")
#> [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE
库(tidyverse)
#创建示例数据:
测试数量:6 x 1
#>a
#>   
#> 1     1
#> 2     2
#> 3     3
#> 4    22
#> 5    23
#> 6    24
测试字符:6 x 1
#>a
#>   
#> 1 1    
#> 2 2    
#> 3 3    
#> 4 22   
#> 5 23   
#> 6 24
#分段数字柱工程:
测试数量%>%
dplyr::filter(a,stringr::str_detect(a,“\\d{2,}”))
#>#tibble:3 x 1
#>a
#>   
#> 1    22
#> 2    23
#> 3    24
#对字符列进行子集设置不起作用:
测试字符%>%
dplyr::filter(a,stringr::str_detect(a,“\\d{2,}”))
#>筛选器_impl(.data,quo)中出错:计算错误:只能对数字、逻辑或复杂类型执行操作。
#当通过访问列进行子集设置时
#使用`$`运算符可以:
测试字符$a%>%
stringr::str_detect(\\d{2,})
#>[1]假假假真真
测试数量$a%>%
stringr::str_detect(\\d{2,})
#>[1]假假假真真

关于问题可能是什么以及如何使用
filter()
方法解决这个问题,您有什么想法吗?非常感谢您事先的帮助

只需取出筛选调用中的第一个
a

而不是:

test_char %>%
  filter(a, str_detect(a, "2"))
使用:

应该有用

过滤器函数中的第一个也是唯一一个参数应该是str_detect(col,“string”)


希望有帮助

请尝试
test_char%>%dplyr::filter(stringr::str_detect(a,“\\d{2,}”)
我明白了,但我想知道为什么可以对数字列进行子集划分?(当然还要感谢你的回答!)谢谢你的回答,但是我仍然很困惑为什么
filter()
可以在数字列上工作,即使从技术上来说这不应该工作?我相信对于数字向量(比如test_num$a),
str_detect()
会强制使用字符类型进行字符串检测,尽管它在技术上不是一个“字符串”。
test_char %>%
  filter(str_detect(a, "2"))