R - 删除字符中长度为一的字符串和停用词使用 tidytext 数据

问题描述

如果我有一个 df:

   Class sentence
1   Yes  there is p beaker on the table
2   Yes  they t the frown
3   Yes  so Z it was asleep


如何删除“句子”列中长度为一的字符串以删除诸如“t”、“p”和“Z”之类的内容,然后使用 tidytext 中的 stop_words 列表进行最后清理以获得以下内容

   Class sentence
1   Yes  beaker table
2   Yes  frown
3   Yes  asleep

解决方法

如果我们想使用tidytext,那么创建一个序列列(row_number()),然后在unnest_tokens列上应用sentence,做一个anti_join使用get_stopwords()的默认数据,filter取出只有1个字符的单词,然后在'word'列上按paste进行分组,创建'sentence'>

library(dplyr)
library(tidytext)
library(stringr)
df %>% 
   mutate(rn = row_number()) %>%
   unnest_tokens(word,sentence) %>% 
   anti_join(get_stopwords()) %>% 
   filter(nchar(word) > 1) %>%
   group_by(rn,Class) %>%
   summarise(sentence = str_c(word,collapse = ' '),.groups = 'drop') %>% 
   select(-rn)

-输出

# A tibble: 3 x 2
  Class sentence    
  <chr> <chr>       
1 Yes   beaker table
2 Yes   frown       
3 Yes   asleep      

数据

df <- structure(list(Class = c("Yes","Yes","Yes"),sentence = c("there is p beaker on the table","they t the frown","so Z it was asleep")),class = "data.frame",row.names = c("1","2","3"))

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...