r - R从文本中删除停用词而不将数据标记和转换为列表
问题描述
我需要从文本中删除停用词而不将对象标记或更改为列表。使用 rm_stopwords 函数时出现错误。谁能帮我吗?
test<- data.frame(words = c("hello there, everyone", "the most amazing planet"), id = 1:2)
test$words <- rm_stopwords(test$words, tm::stopwords("english"), separate = F, unlist = T)
#Error in `$<-.data.frame`(`*tmp*`, words, value = c("hello", "everyone", :
#replacement has 4 rows, data has 2
#I want something like this, where the stopwords are removed but the rest of the formatting remains intact (e.g. punctuation)
# words id
#1 hello , everyone 1
#2 amazing planet 2
解决方案
试试这种方法,它会产生类似于你想要的输出。您可以使用tidytext
函数根据停用词制作过滤器,然后将过滤后的值融合到接近您期望的数据框中。这里的代码:
library(tidytext)
library(tidyverse)
#Data
test<- data.frame(words = c("hello there, everyone", "the most amazing planet"),
id = 1:2,stringsAsFactors = F)
#Unnest
l1 <- test %>% unnest_tokens(word,words,strip_punct = FALSE)
#Vector for stop words
vec<-tm::stopwords("english")
#Filter
l1<-l1[!(l1$word %in% vec),]
#Re aggregate by id
l2 <- l1 %>% group_by(id) %>% summarise(text=paste0(word,collapse = ' '))
输出:
# A tibble: 2 x 2
id text
<int> <chr>
1 1 hello , everyone
2 2 amazing planet
推荐阅读
- hyperledger-fabric - 错误:[Orderer.js]:sendBroadcast - 错误:“错误:14 UNAVAILABLE:.. Hyperledger Caliper
- angularjs - 赛普拉斯剑道下拉列表
- javascript - 使用 css 显示
- node.js - Node js中路由器下的模拟功能
- mongodb - 在 MongoDB 中,如何增加多个值?
- web-deployment - 尝试连接到 heroku 数据库,甚至使用所有凭据配置 pg_hba.conf 文件但收到此错误
- javascript - ES5 IIFE 与 ES6 块
- reactjs - 使用 i18n 更改语言时使用 react-leaflet 更新 GeoJson 上的工具提示
- ssas - 从 SSAS 多维立方体迁移到表格模型
- excel - 如何在 VBA 中调试平方根溢出(MacOS v.16.36 上的错误)