R Tidytext and unnest_tokens error

自闭症网瘾萝莉.ら 提交于 2019-12-21 21:26:07

问题


Very new to R and have started to use the tidytext package.

I'm trying to use arguments to feed into the unnest_tokens function so I can do multiple column analysis. So instead of this

library(janeaustenr)
library(tidytext)
library(dplyr)
library(stringr)

original_books <- austen_books() %>%
  group_by(book) %>%
  mutate(linenumber = row_number(),
         chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
                                                 ignore_case = TRUE)))) %>%
  ungroup()

original_books

tidy_books <- original_books %>%
              unnest_tokens(word, text)

The last line of code would be:

output<- 'word'
input<- 'text'

tidy_books <- original_books %>%
              unnest_tokens(output, input)

But I'm getting this:

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.

I've tried using as.character() without much luck.

Any ideas on how this would work?


回答1:


Try

tidy_books <- original_books %>% 
              unnest_tokens_(output, input)

with the underscore in unnest_tokens_.

unnest_tokens_ is the "standard evaluation" version of unnest_tokens, and allows you to pass in variable names as strings. See Non-standard evaluation for a discussion of standard vs non-standard evaluation.



来源:https://stackoverflow.com/questions/39217789/r-tidytext-and-unnest-tokens-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!