首页 > 解决方案 > 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
}

注意

照原样,循环将执行接近 16,000,000,000 次。那是非常低效的,我怎样才能让这个循环更有效率,这样我就不必等待一个月才能完成?

标签: rperformanceloops

解决方案


如果我没记错的话,这就是你要找的那个吗?

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

推荐阅读