【发布时间】:2020-09-05 15:02:32
【问题描述】:
当我为我手动输入的列表取消嵌套令牌时;输出包括每个单词来自的行号。
library(dplyr)
library(tidytext)
library(tidyr)
library(NLP)
library(tm)
library(SnowballC)
library(widyr)
library(textstem)
#test data
text<- c( "furloughs","Working MORE for less pay", "total burnout and exhaustion")
#break text file into single words and list which row they are in
text_df <- tibble(text = text)
tidy_text <- text_df %>%
mutate_all(as.character) %>%
mutate(row_name = row_number())%>%
unnest_tokens(word, text) %>%
mutate(word = wordStem(word))
结果是这样的,这正是我想要的。
row_name word
<int> <chr>
1 1 furlough
2 2 work
3 2 more
4 2 for
5 2 less
6 2 pai
7 3 total
8 3 burnout
9 3 and
10 3 exhaust
但是当我尝试从 csv 文件中读取真实响应时:
#Import data
text <- read.csv("TextSample.csv", stringsAsFactors=FALSE)
但否则使用相同的代码:
#break text file into single words and list which row they are in
text_df <- tibble(text = text)
tidy_text <- text_df %>%
mutate_all(as.character) %>%
mutate(row_name = row_number())%>%
unnest_tokens(word, text) %>%
mutate(word = wordStem(word))
我将整个令牌列表分配给第 1 行,然后再次分配给第 2 行,依此类推。
row_name word
<int> <chr>
1 1 c
2 1 furlough
3 1 work
4 1 more
5 1 for
6 1 less
7 1 pai
8 1 total
9 1 burnout
10 1 and
或者,如果我将 mutate(row_name = row_number) 移到 unnest 命令之后,我会得到每个标记的行号。
word row_name
<chr> <int>
1 c 1
2 furlough 2
3 work 3
4 more 4
5 for 5
6 less 6
7 pai 7
8 total 8
9 burnout 9
10 and 10
我错过了什么?
【问题讨论】:
-
由于我们讨论的是读取数据时的问题,如果您发布原始文件的前 11 行(未在帧中解析)可能会提供信息。手动编辑并复制,或使用
dput(readLines("TextSample.csv", n=11))。谢谢。
标签: r row-number tidytext unnest