首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >unnest_tokens及其错误(“”)

unnest_tokens及其错误(“”)
EN

Stack Overflow用户
提问于 2017-07-20 16:37:54
回答 1查看 4.8K关注 0票数 1

我在处理tidytext。当我命令unnest_tokens时。R返回错误

请提供列名

如何解决这个错误?

代码语言:javascript
复制
library(tidytext)
library(tm)
library(dplyr)
library(stats)
library(base)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
  #Build a corpus: a collection of statements
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
f <-Corpus(DirSource("C:/Users/Boon/Desktop/Dissertation/F"))
doc_dir <- "C:/Users/Boon/Desktop/Dis/F/f.csv"
doc <- read.csv(file_loc, header = TRUE)
docs<- Corpus(DataframeSource(doc))
dtm <- DocumentTermMatrix(docs)
text_df<-data_frame(line=1:115,docs=docs)

#This is the output from the code above,which is fine!: 
# text_df
# A tibble: 115 x 2
#line          docs
#<int> <S3: VCorpus>
# 1      1 <S3: VCorpus>
#2      2 <S3: VCorpus>
#3      3 <S3: VCorpus>
#4      4 <S3: VCorpus>
#5      5 <S3: VCorpus>
#6      6 <S3: VCorpus>
#7      7 <S3: VCorpus>
#8      8 <S3: VCorpus>
#9      9 <S3: VCorpus>
#10    10 <S3: VCorpus>
# ... with 105 more rows

unnest_tokens(word, docs)

# Error: Please supply column name
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-21 18:47:49

如果要将文本数据转换为整洁的格式,则不需要首先将其转换为语料库或文档术语矩阵或其他任何内容。这是为文本使用整洁数据格式的主要思想之一;除非您需要建模,否则不使用这些其他格式。

您只需将原始文本放入数据框架,然后使用unnest_tokens()对其进行整理。(我在这里对您的CSV的外观做了一些假设;下次发布reproducible example会更有帮助。)

代码语言:javascript
复制
library(dplyr)

docs <- data_frame(line = 1:4,
                   document = c("This is an excellent document.",
                                "Wow, what a great set of words!",
                                "Once upon a time...",
                                "Happy birthday!"))

docs
#> # A tibble: 4 x 2
#>    line                        document
#>   <int>                           <chr>
#> 1     1  This is an excellent document.
#> 2     2 Wow, what a great set of words!
#> 3     3             Once upon a time...
#> 4     4                 Happy birthday!

library(tidytext)

docs %>%
    unnest_tokens(word, document)
#> # A tibble: 18 x 2
#>     line      word
#>    <int>     <chr>
#>  1     1      this
#>  2     1        is
#>  3     1        an
#>  4     1 excellent
#>  5     1  document
#>  6     2       wow
#>  7     2      what
#>  8     2         a
#>  9     2     great
#> 10     2       set
#> 11     2        of
#> 12     2     words
#> 13     3      once
#> 14     3      upon
#> 15     3         a
#> 16     3      time
#> 17     4     happy
#> 18     4  birthday
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45220536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档