- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 dplyr::filter()
、stringr::str_detect
和 对字符列
- 使用正则表达式捕获两位或更多数字的管道。 a
进行子集化>magrittr
这似乎只适用于数字列,并且仅在使用 $
- 运算符直接访问该列时才有效:
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
关于问题可能是什么以及如何使用 filter()
方法解决这个问题的任何想法?非常感谢您的提前帮助!
最佳答案
只需取出过滤器调用中的第一个 a
。
代替:
test_char %>%
filter(a, str_detect(a, "2"))
使用:
test_char %>%
filter(str_detect(a, "2"))
应该可以。
过滤器函数中的第一个也是唯一一个参数应该是 str_detect(col, "string")
。
希望对您有所帮助!
关于r - 使用 stringr 的 str_detect() 过滤字符向量的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058519/
我有像c1c5, c5c1, c4c3 ...这样的colnames ... 我想检索所有以 c4 和 c5 开头或结尾的 colnames。 我试过使用以下方法: str_detect(colnam
我想使用 across 根据跨多个列的 str_detect 结果创建一个新列。 例如,在下面的测试数据中,我想在以“job”开头的列中搜索“No job”,然后如果在任何列中检测到该字符串则返回 1
我有两个数据集,一个是全名,一个是名字和姓氏。 library(tidyverse) (x = tibble(fullname = c("Michael Smith",
获取字符串列表: strings % .[!map_lgl(., str_detect, avoid)] [1] "qwe_xyz" "XYZ" 不过我想做的是指定几个子字符串 avoid_2
我想过滤掉列中包含字符串的行。我正在使用 tidyverse 解决方案。我遇到的问题是 str_detect 似乎也在寻找 NA 结果,因此这些也被我的过滤器删除了: df1 = data.frame
我目前有一个具有以下结构的 data.frame (X): Number Observation 1 34 2 Example 3 Example34% 4 Example 5 3
如您所见,我正在处理一些严重的脏数据。这段代码可以工作,但看起来有点笨拙。有没有一种更高效、更动态的方式来实现最终结果而不需要那么多编码? 我必须分阶段执行此操作,首先标记内容类型,然后利用内容类型将
如您所见,我正在处理一些严重的脏数据。这段代码可以工作,但看起来有点笨拙。有没有一种更高效、更动态的方式来实现最终结果而不需要那么多编码? 我必须分阶段执行此操作,首先标记内容类型,然后利用内容类型将
我正在使用功能 str_detect从 tidyverse 过滤掉与列表中任何字符串开头匹配的数据帧的行。目前,正在使用 |在我的 filter 中的每一列之间过滤时的语句陈述。有什么方法可以利用st
我正在尝试使用 str_detect 检测字符串是否包含特定模式。我的图案是一系列“....” - 确切的点数未知。我正在尝试使用 str_detect 如下.... 但是,在这种特殊情况下,str_
样本数据 files.in.path = c("a.4.0. name 2015 - NY.RDS", "b.4.0. name 2016 - CA.RDS",
我正在尝试使用 str_detect 的简写和 &至 filter一个数据框: library(tidyverse) df % filter(str_detect(type, "age") & s
我想找到多个字符串并将其放入一个变量中,但是我不断收到错误消息。 queries % filter(str_detect(payload, "create" || "drop" || "select"
我有以下数据集: df % filter(str_detect(a, "F")) 但是我得到了这个 Error in stri_detect_regex(string, pattern, op
我有以下数据集: df % filter(str_detect(a, "F")) 但是我得到了这个 Error in stri_detect_regex(string, pattern, op
我有一个 Shiny 的应用程序,我希望每次用户在先前的 selectInput 中选择包含特定单词(但不是确切单词)的字符串时,都会出现一个条件面板。这是我目前拥有的: library(shiny)
我在 R 中有一个字符串: c("FLT1", "FLT1P1", "FLT1-FLT2", "SGY-FLT1, GPD") 我想保留所有具有 FLT1 的匹配项,但在添加其他字母数字 字符时不保留
我正在尝试使用 dplyr::filter()、stringr::str_detect 和 对字符列 a 进行子集化>magrittr - 使用正则表达式捕获两位或更多数字的管道。 这似乎只适用于数字
我想使用 filter() 和 str_detect() 匹配多个模式来过滤数据帧,而无需多个 str_detect() 函数调用。在下面的示例中,我想过滤数据框 df仅显示包含字母 a 的行f和 o
我们正在尝试解析包含 unicode 的名称向量,但得到了一些奇怪的结果。当我们搜索 \xf6 时,下面的向量返回三个,而不是两个 TRUE 值。我们缺少什么? library(tidyverse)
我是一名优秀的程序员,十分优秀!