首页 > 解决方案 > 为什么我的停用词不会删除 R 数据框中的“?&”?

问题描述

我在数据框中有一列,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))

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

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

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

标签: rdataframestop-words

解决方案


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

Base R解决方案:

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

stringr解决方案:

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

推荐阅读