【问题标题】:Remove words using PURRR from dataframe in R使用 PURRR 从 R 中的数据框中删除单词
【发布时间】:2018-04-10 08:29:14
【问题描述】:

我见过使用gsubsapply 从数据框中删除单词的示例。

有没有使用purrr库中的map的解决方案

library(purrr)
ID<-c(1,2)
Text_W<-c("I love vegetables, and some fruits","Can we meet tomorrow for a movies, and other fun activities") 
new_tab<-tibble(ID,Text_W)
remove_words<-c("love", "and")

我尝试了这些但没有成功:

#gsub from base
map_chr(new_tab$Text_W,~paste(gsub(remove_words,"")))


library(stringr)
#
map(new_tab$Text_W,~paste(str_replace_all(remove_words,"")))

任何帮助将不胜感激。

【问题讨论】:

    标签: r lapply sapply stringr purrr


    【解决方案1】:

    不必使用map。试试看

    new_tab %>% 
      mutate(Text_New=str_replace_all(Text_W, paste(remove_words,collapse = "|"),""))
    # A tibble: 2 x 3
         ID Text_W                                                      Text_New                                                
      <dbl> <chr>                                                       <chr>                                                   
    1    1. I love vegetables, and some fruits                          I  vegetables,  some fruits                             
    2    2. Can we meet tomorrow for a movies, and other fun activities Can we meet tomorrow for a movies,  other fun activities
    

    请注意,我使用 paste(remove_words,collapse = "|") 折叠了 remove_wordsor == | 参数。

    【讨论】:

    • 感谢您的宝贵帮助。快速提问 - 如果我将 remove_words
    【解决方案2】:

    您主要是错过了.来引用函数参数:

    > map_chr(new_tab$Text_W, ~gsub("love|and", "", .))
    [1] "I  vegetables,  some fruits"                             
    [2] "Can we meet tomorrow for a movies,  other fun activities"
    

    还要注意gsub("love|and" 而不是gsub(c("love","and")

    编辑

    如果您想使用要删除的单词向量,而不是输入love|and,请执行

    map_chr(new_tab$Text_W, ~gsub(paste(remove_words, collapse="|"), "", .))
    

    【讨论】:

    • 为什么要使用粘贴?有没有需要
    • @StéphaneLaurent - 如何在 R 中以编程方式将 remove_words 变为“love|and”。在我的真实示例中,我使用的是 tm::stopwords("english")。如何将此列表转换为“x|y|z|..”格式。谢谢
    • @Beginner 不确定是否理解。也许paste(remove_words,collapse = "|"),就像@Jimbou 的回答一样。
    • @StéphaneLaurent 完美 - 谢谢。 tm::stopwords("english") 中的单词之一是 or。如果 remove_words 更新为包含“或”... remove_words
    • @Beginner 我明白你的意思。也许简单地添加空格:`remove_words
    【解决方案3】:

    我会使用其中一种方式,不会使用purrr 来处理这种方式

    library(purrr)
    library(dplyr)
    library(stringr)
    
    ID<-c(1,2)
    Text_W<-c("I love vegetables, and some fruits","Can we meet tomorrow for a movies, and other fun activities") 
    new_tab<-tibble(ID,Text_W)
    remove_words<-c("love", "and")
    
    # This is basic, if you are only doing it for one column, see Jimbou's note on collapse
    new_tab %>% 
      mutate(Text_W = str_replace_all(Text_W, paste(remove_words,collapse = "|"),""))
    
    # This is more scalable, as you can put other columns in the `vars()` method
    new_tab %>% 
      mutate_at(vars(Text_W), str_replace_all, paste(remove_words, collapse = "|"), "")
    
    # This is is scalable, but uses base R in case I didn't feel like having to load stringr
    new_tab %>% 
      mutate_at(vars(Text_W), sub, 
                pattern = paste(remove_words, collapse = "|"), 
                replacement = "")
    

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多