微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 DoParallel R 将 FOR 循环更改为 FOR EACH 循环

如何解决使用 DoParallel R 将 FOR 循环更改为 FOR EACH 循环

我正在尝试使用 doParallel 将我的 FOR 循环更改为 FOR EACH 循环。有人可以帮助我将第一行更改为 FOR EACH 设置的语法吗?我对如何措辞感到困惑,以便其他循环仍将取决于“gro”。我相信这个脚本的很多内容也可以简化,但我现在并不太担心。

这是我的原始脚本:

for(gro in 1:length(SearchTerms)){
      # Recall the phrase terms for the specified group,#   pulls from SearchTerms
      PhraseTerms <- SearchTerms[gro][1]
      # Starts For Loop
      #   Searches all PhraseTerms within the group
      for(PhraseTerms in SearchTerms[gro][1]){
        # Create character vector of PhraseTerms
        PhraseTerms<- unlist(PhraseTerms)
        # Create variable for specified term. This is the
        #   X term of the specified group (EX: first term
        #   of the 1st group will be first)
        Pterm <- PhraseTerms[1]
        # Starts For Loop
        # Searches ALL TERMS within the group of iteration
        for( Pterm in PhraseTerms){
          # Creates a "named character"
          Aterm <- setNames(nm = Pterm)
          # Creates df,sets up for next line
          Bterm <- data.frame(Aterm)
          # Changes Pterm to all UPPER CASE
          Bterm %>% mutate_if(is.character,str_to_upper) -> Bterm
          # Creates a "named character" which is needed for the count1 line to work
          Aterm <- setNames(nm = paste(Bterm$Aterm))
          # Counts ALL of the matches of Pterm found within the MCN ID phrase
          # ACTUAL CODE
          count1 <- data.frame(sapply(Aterm,str_count,string = MCN_Phrase$Verbage))
          # TEST CODE
          #count1 <- data.frame(sapply(Aterm,string = MCN_Phrase$Verbage))
          # Creates running count of how many matches are found
          count <- count + count1$sapply.Aterm..str_count..string...MCN_Phrase.Verbage.
        } 
      }
    }

    Classscore <- data.frame(MCN_Phrase$MCN_NEW.MCN_ID.abc.,count)
    colnames(Classscore)[1] <- "MCN_ID"
    colnames(Classscore)[2] <- paste("UNS",UNS,sep = " ")

  
    Result_df <- bind_rows(Result_df,Classscore)
    
    Result_df <- Result_df %>%
      group_by(MCN_ID) %>%
      summarise_each(funs(first(na.omit(.))))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。