r - 数据框每一行的字符串的部分匹配
问题描述
以虹膜数据为例来说明我的问题,我想对“.5”进行部分匹配并获取位置的索引(在我的真实数据中,0.5 实际上是一个字符串“_mutations”)。
我打算遍历每一行,执行部分匹配,获取第一个匹配的索引。我使用了以下内容;
idx = regexpr(pattern, txt[i,], ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)[1]
idx = regexec(pattern, txt[i,], ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)[1]
gregexpr(pattern, txt[j,], ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
stri_locate_first_regex(txt[i,], pattern)
str_detect(txt[i,], pattern)
示例数据如下所示;
library(ggplot2)
txt = iris
pattern=".5"
预期结果是第一个匹配的索引。
解决方案
将所有值替换TRUE
为匹配项
df <- iris
# [] notation preserves structure
df[] <- lapply(X = df, function(x) {
grepl(pattern = ".5",
x = as.character(x),
fixed = TRUE)
})
获取TRUE
每列值的位置
sapply(X = df, which)
结果
# $Sepal.Length
# [1] 34 37 42 54 55 81 82 90 91 105 111 117 148
#
# $Sepal.Width
# [1] 1 18 28 37 41 44 70 73 90 99 107 109 114 147
#
# $Petal.Length
# [1] 4 8 10 11 16 20 22 28 32 33 35 40 49 52 56 61 67 69 79 80
# [21] 85 86 107 113 117 138
#
# $Petal.Width
# [1] 24 52 53 55 62 67 69 73 79 85 87 101 110 120 134 145
#
# $Species
# integer(0)
笔记
有很多方法可以解决这个问题。我喜欢这个解决方案,因为结果可读性很强,但我认为这在很大程度上是一个品味问题。
推荐阅读
- javascript - 带有提示的 Cookie
- sapui5 - 为 SAPUI5 应用程序实施内联帮助/导览
- javascript - 是的,验证不同对象形状的数组
- react-native - 刷新内容中的平面列表
- javascript - 物化 CSS 轮播指示器不可点击
- javascript - PHP 从 React JS 发送 FormData 后返回 NULL(Fetch,方法:POST)
- android - 使用没有自定义适配器的 Moshi 解析列表
- swift - SwiftUI 中的单元测试
- c# - Nreco 立方体槽加工
- javascript - (新)来自嵌套组件的反应上下文不起作用