使用txt文件作为源时的Tidytext unnest_tokens错误

问题描述 投票:0回答:1

对这个主题来说是新的。我在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.

有人能解决我的问题吗?预先谢谢!

r text-mining tidytext
1个回答
0
投票

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

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         
#...
#....
© www.soinside.com 2019 - 2024. All rights reserved.