You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中移除停用词时遇tm_map方法不适用错误求助

问题原因

tm_map 是tm包专门用来处理**语料库(Corpus)**类型对象的函数,但你用scan读入的comments是字符向量(class为character),类型不匹配,所以触发这个错误——和你调整索引、修改括号的操作无关。

解决步骤
  1. 先加载tm包(如果没安装先运行install.packages("tm")):
library(tm)
  1. 把你的待处理文本转换成tm包要求的Corpus对象(假设你的文本数据存在名为df的数据框的text列中):
# 从文本列创建语料库
corpus <- VCorpus(VectorSource(df$text))
  1. 正确读取停用词列表:
mystopwords <- scan(file = "mystopwords.data", what = character(), sep = '\n')
# 提取你需要的停用词范围
target_stopwords <- mystopwords[2:671]
  1. 对Corpus对象执行移除停用词操作:
corpus_clean <- tm_map(corpus, removeWords, target_stopwords)

额外提醒:你代码里的变量名存在混淆——comments应该指代你的待处理文本语料,而mystopwords是停用词列表,不要搞反两者的用途。

内容的提问来源于stack exchange,提问作者Nicholas Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:30:53