【问题标题】:How to resolve "Error in gsub" with removeWords in R如何使用 R 中的 removeWords 解决“gsub 中的错误”
【发布时间】:2020-05-19 03:59:43
【问题描述】:

我有一个包含推文的数据框。我正在努力删除停用词,因此我使用了:

stopWords <- stopwords("en")
tweets_sample$text<-removeWords(tweets_sample$text,stopWords)

反正我得到了

Error in gsub(sprintf("(*UCP)\\b(%s)\\b", paste(sort(words, decreasing = TRUE),  : 
input string 1 is invalid UTF-8

出现这种错误的原因是什么?

【问题讨论】:

  • tweets_sample
  • 当我的输入包含\uFFFF char 时遇到了这个问题。

标签: r gsub tm


【解决方案1】:

看起来像是编码问题。尝试Encoding(tweets_sample$text) &lt;- "UTF-8",然后尝试removeWords 函数。

【讨论】:

    【解决方案2】:

    您的第一个字符串中的 UTF-8 似乎无效。 您可以使用 iconv 替换文本中任何不可转换的字节:

    text <- "your text"
    Encoding(te\xE7xt) <- "UTF-8"
    iconv(text, "UTF-8", "UTF-8",sub='') 
    

    “文字”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2021-04-03
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多