r - R中是否有一种有效的方法来搜索和替换小标题中多个字符串中的单词?
问题描述
我有一个小标题,在这个小标题中我有一个名为“描述”的列。这里有大约 380,000 条描述。
描述示例:
“缩写非常有用”
这只是让您熟悉我的数据的一个示例。所有的描述都不一样。
我也有一个拼写正确的单词。大约有 42,000 个唯一正确拼写的单词。
我的任务是用正确拼写的单词替换描述中所有拼写错误的单词。所以“hlpful”这个词将被“helpful”取代。
我的代码如下:
countKeyWords <- 1
countDescriptions <- 1
amountKeyWords <- 42083
amountDescriptions <- 379571
while (countKeyWords < amountKeyWords){
while (countDescriptions < amountDescriptions){
semiFormatTet$description[countDescriptions] <-
gsub(keyWords$SearchFor[countKeyWords], keyWords$Map[countKeyWords], semiFormatTet$description[countDescriptions], ignore.case = TRUE)
countDescriptions = countDescriptions + 1
}
countDescriptions = 0
countKeyWords = countKeyWords + 1
}
注意:
SearchFor:拼写正确的单词的前缀,用于与描述中拼写错误的单词进行比较。
地图:将替换拼写错误的单词的正确拼写单词。
照原样,循环将执行接近 16,000,000,000 次。那是非常低效的,我怎样才能让这个循环更有效率,这样我就不必等待一个月才能完成?
解决方案
如果我没记错的话,这就是你要找的那个吗?
Library(stringr)
Library(Tidyverse)
Library(dplyr)
df <- data.frame(DESCRIPTION = c("This is the first description with hlpful",
"This is the second description with hlpful",
"This is the third description with hlpful",
"This is the fourth description with hlpful",
"This is the fifth description with hlpful",
"This is the sixth description with hlpful",
"This is the seventh description with hlpful",
"This is the eighth description with hlpful",
"This is the ninth description with hlpful"))
df$DESCRIPTION <- str_replace_all(df$DESCRIPTION,"hlpful", "helpful")
DESCRIPTION
1 This is the first description with helpful
2 This is the second description with helpful
3 This is the third description with helpful
4 This is the fourth description with helpful
5 This is the fifth description with helpful
6 This is the sixth description with helpful
7 This is the seventh description with helpful
8 This is the eighth description with helpful
9 This is the ninth description with helpful
推荐阅读
- javascript - 尽管路径明显正确,如何修复 Node.js 文件路径错误?
- javascript - 在 React 组件中设置多个 Google Map 引脚
- html - 我有一个 html 内容,它占用了页面的完整宽度,但在 PDF 中它只占用了 50% 的宽度
- android - Android将listview数据转换为pdf文件并保存
- angular - Angular File Input 所选文件未显示
- mips - MIPS 嵌套过程
- java - 交易块之外会发生什么?
- c# - 如何增加 CalendarDatePicker 的日历图标大小并使用不同的文本或图像图标 [UWP] 对其进行自定义?
- c++ - 访问 range_expression 中的嵌套元素会返回不完整的映射(段错误)
- node.js - Nodejs 错误:“用户对此配置文件没有足够的权限”