使用 txt 文件作为源时出现 R Tidytext unnest_tokens 错误

R Tidytext unnest_tokens error when using a txt file as source

这个话题很新。我在使用 tidytext 包中的 unnest_tokens 函数时遇到问题。我有一些要分析的 .txt 格式的文本。

例如将以下句子放入 txt 文件中,然后将其读入 R:

Emily Dickinson wrote some lovely text in her time.

text <- c("Because I could not stop for Death -",
          "He kindly stopped for me -",
          "The Carriage held but just Ourselves -",
          "and Immortality")

下面是我的代码:

library(dplyr)
library(tidytext)
library(readtext)
my_data <- read_file("exp.txt")
my_data_tibble <- tibble(text = my_data)
my_data_tibble %>% 
  unnest_tokens(word, my_data)

然后我会收到以下错误信息:

Error in check_input(x) : 
  Input must be a character vector of any length or a list of character
  vectors, each of which has a length of 1.

有人能解决我的问题吗?提前致谢!

第一个输入是你想要的输出列的列名,第二个是输入的列名。

library(tidytext)

my_data_tibble %>% unnest_tokens(word, text)

# A tibble: 20 x 1
#   word       
#   <chr>      
# 1 because    
# 2 i          
# 3 could      
# 4 not        
# 5 stop       
# 6 for        
# 7 death      
# 8 he         
#...
#....