【问题标题】:Why won't my stopwords delete '?&' in an R data frame? [duplicate]为什么我的停用词不会删除 R 数据框中的“?&”? [复制]
【发布时间】:2021-09-11 19:05:15
【问题描述】:

我在数据框中有一列,old_df

示例行如下所示:

data
trying URL 'https://maps.googleapis.com/maps/api/streetview?&location=13.5146367326733,100.380686367492&size=8000x5333&heading=0&fov=90&pitch=0&key='Content type 'image/jpeg' length 59782 bytes (58 KB)
downloaded 58 KB

使用stopwords,我删除了我不想要的词,并留下:

data
?&13.5146367326733,100.380686367492
?&13.5162026732673,100.66581378616

stopwords = c('trying',
          'URL', 
          "'",
          '&',
          'location=',
          'https://maps.googleapis.com/maps/api/streetview',
          'size=8000x5333',
          'heading',
          '=0&fov=90&pitch=0&key=',
          'Content', 
          'type',
          'image/jpeg',
          'length', 
          'bytes',
          'KB')

require('tm')
new_df <- as.data.frame(removeWords(old_df$data, stopwords))

但是,?&amp; 仍保留在数字之前的 data 列中(我不想要)。我尝试在stopwords 中包含?&amp;?&amp;,但它们仍然存在。任何想法如何删除它们?

确实,当我在stopwords 中包含上述组合时,我得到了错误:

PCRE pattern compilation error 'quantifier does not follow a repeatable item' at '?|&amp;|')\b'

【问题讨论】:

    标签: r dataframe stop-words


    【解决方案1】:

    使用gsub()。停用词只删除被空格包围的“单词”。

    Base R解决方案:

    gsub("^\\?&", "", old_df$data)
    

    stringr解决方案:

    library(stringr)
    stringr::str_remove(old_df$data, "^\\?&")
    

    【讨论】:

    • 谢谢,但奇怪的是这些解决方案不会删除我的字符串中的?&amp;。但是,使用gsub('?&amp;', '', old_df$data) 会返回?13.5146367326733,100.380686367492。所以现在我们只需要? 就可以了,但我再次不确定为什么/如何保持这种状态。
    • 你需要用\\?转义问号。查看我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 2021-07-11
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2018-02-08
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多