r - 大型数据集 R 的两列之间的部分字符串匹配
问题描述
我有两列,如果两列之间存在部分匹配,我想创建一个二进制列。
例如:
X Y Match
hello hello 1
hi hello hi 1
NA bye NA
bye hi bye 1
good bad 0
我使用了以下代码,
df['Match'] <- ifelse(with(df, str_detect(x, y)|str_detect(y, x)), 1, 0)
这适用于前几行,但是当我在整个数据集(n = 14000)上使用它时,我不断收到此错误:
Error in stri_detect_regex(string, pattern, opts_regex = opts(pattern)) :
Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
我应该如何解决这个问题?
解决方案
您的数据中可能有括号或导致此错误的特殊字符。
尝试这样的循环:
for(i in 1:nrow(df)) {
print(i)
str_detect(df$x[i], df$y[i])
}
最后i
打印的将告诉您问题出在哪一行。
推荐阅读
- javascript - 如何使用 css 属性 jquery 弹出窗口
- visual-studio - Visual Studio 2017 - 运行具有大文件大小的 Azure 函数时出现 System.OutOfMemoryException
- java - 模拟开假反应体
- java - Can I instantiate the Class object using its name
- jmeter - 通过发送相同的请求,我需要特定数量的响应数据“成功”
- python - 运行 model.fit 时不显示指标
- scala - 使用 immutable.ListMap 代替 mutable.LinkedHashMap
- json - 如何在json中编写site_url链接
- java - 内部类可见性的一些问题
- android - 如何使用android中的任何布局将两个文本视图与背景水平对齐?